问题描述
以下c ++ 0x / c ++ 11功能(初始化列表,包括对于STL容器)应该允许我在声明时初始化此STL std :: map
。并且,它的工作原理很好在g ++ 4.7.2,但我得到一个错误尝试编译它在g ++ 4.4.6。根据GCC文档,这个c ++ 0x功能在4.4中可用,但是显然(a)我做错了,或者(b)它在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 filename}}
};但是,当我尝试编译是在gcc 4.4.6下,我得到以下<$ c $ c>无匹配函数调用database.cc:164:error:没有匹配函数调用'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(<括号括起始化程序列表>)'
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include /c++/4.4.6/bits/stl_map.h:195:注意:候选是: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:注意:std :: map< _Key,_Tp,_Compare,_Alloc> :: map(std :: map< _Key,_Tp,_Compare,_Alloc>&&&)[with _Key = std :: basic_string& 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_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> > > > > >]
(是否可以更改粘贴格式,多少水平滚动?)
无论如何。在这一点上,我试图找出我要做不同的,以使这个初始化在g ++ 4.4.6工作。对于我声明
std :: map
或std :: vector
的方式,这会解决问题吗?或者必须使用完全不同的方法,因为在g ++ 4.4中对这个c ++ 11特性的不同和/或不完全的实现?解决方案您的代码正确无误。 g ++ - 4.4官方支持初始化列表。但我记得(我遵循wg21文件),有一个关注,特别是无法初始化容器在某种程度上你在做的事情(嵌套)在某种程度上。
很可能,由于这些更改之后做的更改,g ++ - 4.4没有完全支持
std :: initializer_list
语法。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 thestd::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 ?解决方案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.这篇关于g ++ STL容器initializer_list行为之间的差异在4.4和4.7之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!