| 689 | |
| 690 | === C++11 language constructs === |
| 691 | |
| 692 | As C++11 is the base language standard that the MapGuide/FDO codebase is expected to be compiled in, you are actively encouraged to take advantage of any language features/constructs introduced with this particular version of C++. |
| 693 | |
| 694 | As MapGuide/FDO was originally conceived before the introduction of C++11, there will be vast places in the codebase where code is written in the pre-C++11 style. You are encouraged (but not required) to migrate such existing code to the C++11 style if you encounter/touch it in your journey of fixing a bug or implementing a new feature. 2 key constructs C++11 constructs worthy of special consideration are detailed below |
| 695 | |
| 696 | ==== `auto` ==== |
| 697 | |
| 698 | Use `auto` where possible to let the C++ compiler infer the type of a local variable based on the type of the right-hand side of a variable assignment. This is most useful when dealing with STL iterators or sufficiently complex templated type where you can avoid having to specify the type like this: |
| 699 | |
| 700 | {{{ |
| 701 | std::vector<int>::iterator it = some_int_vector.begin(); |
| 702 | }}} |
| 703 | |
| 704 | and just declare it like this: |
| 705 | |
| 706 | {{{ |
| 707 | auto it = some_int_vector.begin(); |
| 708 | }}} |
| 709 | |
| 710 | `auto` also has useful applications for primitive variable declarations as it can fix up to one half of any potential `data type mismatch` warnings as a result of declaring the variable with an incorrect (or insufficiently wide) type on the left-hand side of a variable assignment (think `size_t`/`int` indexing mismatches in for loops for an example) |
| 711 | |
| 712 | However, do not use `auto` as the return type (aka. return type deduction) in any public API method signature. We need to be as verbose and concise as possible for SWIG to generate the correct proxy classes. |
| 713 | |
| 714 | ==== for loops ==== |
| 715 | |
| 716 | When working with STL iterators it is much more preferred to use the simplified range-based for loops introduced in C++11 |
| 717 | |
| 718 | Pre C++11 loops look like this: |
| 719 | |
| 720 | {{{ |
| 721 | for (some_type::iterator it = container.begin(); it != container.end(); it++) |
| 722 | { |
| 723 | some_item_type item = *it; |
| 724 | ... |
| 725 | } |
| 726 | }}} |
| 727 | |
| 728 | Such loops can be simplified to this: |
| 729 | |
| 730 | {{{ |
| 731 | for (auto const & item : container) |
| 732 | { |
| 733 | ... |
| 734 | } |
| 735 | }}} |
| 736 | |
| 737 | With adjustments to whether the item needs to be `const` or by reference depending on the mutability needs of the iterated item. |