Changes between Version 1 and Version 2 of CppNotes


Ignore:
Timestamp:
Jan 15, 2016, 1:05:12 AM (4 years ago)
Author:
ddecarli
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CppNotes

    v1 v2  
    33In C we are used to doing things like this:
    44
    5 <pre>
     5{{{
     6#!c
    67struct { int a, b; } map_a2b = { {1, 2}, {2, 4}, {4, 8}};
    7 </pre>
     8}}}
    89
    910To initialize complex data structures.  However if you get clever and try to do it in C++
    1011you get in trouble.  A very simple case works:
    1112
    12 <pre>
     13{{{
     14#!c++
    1315class int_pair {
    1416public:
     
    1618};
    1719int_pair map_a2b[] = { {1, 2}, {2, 4}, {4, 8}};
    18 </pre>
     20}}}
    1921
    2022However a seemingly trivial change breaks it:
    21 <pre>
     23{{{
     24#!c++
    2225class int_pair {
    2326public:
     
    2528};
    2629int_pair map_a2b[] = { {1, 2}, {2, 4}, {4, 8}};
    27 </pre>
     30}}}
    2831
    2932This is because only "aggregate" types may be initialized with lists.
     
    3639You might get clever and try to initialize something more complicated like a map:
    3740
    38 <pre>
     41{{{
     42#!c++
    3943std::map<int,int> a[] = { {1, 2}, {2, 4}, {4, 8}};
    40 </pre>
     44}}}
    4145
    4246This won''t work either, at least not in the current standard (C++98).