2

下面的 c++0x/c++11 特性(初始化列表,包括 STL 容器)应该允许我std::map在声明时初始化这个 STL。而且,它在 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", "password", "filename" } }
};

但是,当我尝试在 gcc 4.4.6 下编译时,我收到以下no matching function call错误,这是典型的“几乎无法理解”的方式:

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> > > > > >]

(是否可以更改粘贴的格式,因此不需要太多的水平滚动?)

反正。在这一点上,我试图弄清楚我必须做些什么才能让这个初始化在 g++ 4.4.6 中工作。std::map关于我声明或 的方式,我可以改变什么来std::vector解决问题吗?或者由于 g++ 4.4 中这个 c++11 功能的实现不同和/或不完整,是否必须使用完全不同的方法?

4

1 回答 1

2

你的代码是正确的。g++-4.4 正式支持初始化列表。但我记得(我关注 wg21 论文)有一个问题,特别是在某些时候无法以您在委员会中所做的方式(使用嵌套)初始化容器。

std::initializer_list由于这些更改是在稍后完成的,g++-4.4 很可能没有完全支持语法。

于 2013-03-30T05:16:16.693 回答