问题
The following c++0x/c++11 feature (initializer lists, including for STL containers) should allow me to initialize this STL std::map
at declaration. And, it works just fine in g++ 4.7.2, but I get an error trying to compile it in g++ 4.4.6. According to the GCC docs, this c++0x feature was available in 4.4, but clearly either (a) I'm doing something wrong, or (b) it was incomplete in g++ 4.4.
std::map<std::string,std::vector<std::string>>
DbConnection::attrs_of_type = {
{ "http", { "url", "ipaddress", "port",
"username", "password" } },
{ "email", { "address", "full_name" } },
{ "ftp", { "hostname", "ipaddress", "port",
"username", "password", "filename" } }
};
But, when I try to compile is under gcc 4.4.6, I get the following no matching function call
error, in it's typical "nearly impossible to comprehend" way:
database.cc:164: error: no matching function for call to ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >::map(<brace-enclosed initializer list>)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:195: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::initializer_list<std::pair<const _Key, _Tp> >, const _Compare&, const _Alloc&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:181: note: std::map<_Key, _Tp, _Compare, _Alloc>::map(std::map<_Key, _Tp, _Compare, _Alloc>&&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:170: note: std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:150: note: std::map<_Key, _Tp, _Compare, _Alloc>::map() [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >]
(is it possible to change how that paste is formatted, so it doesn't require as much horizontal scrolling?)
Anyway. At this point I'm trying to figure out what I have to do differently to get this initialization to work in g++ 4.4.6. Is there something I can change about the way I declare the std::map
, or the std::vector
, that will solve the problem? Or must a wholly different approach be used due to a different and/or incomplete implementation of this c++11 feature in g++ 4.4 ?
回答1:
Your code is correct. g++-4.4 officially supports initializer lists. But I recall (I follow wg21 papers) that there was a concern, specifically about not being able to initialize containers in the way you are doing (with nesting) in the commitee at some point.
It's likely that g++-4.4 has no complete support for std::initializer_list
syntax due to these changes that were done later in time.
来源:https://stackoverflow.com/questions/13400819/differences-in-g-stl-container-initializer-list-behavior-betweeen-4-4-and-4-7