Gos*_*low 5 c++ language-lawyer c++17
我有这个简单的代码:
#include <vector>
#include <string>
void foo(const std::vector<std::pair<std::string, int> > & networks) {
for (auto p : networks) {
}
}
void bla(const std::vector<const std::pair<std::string, int> > & networks) {
for (auto p : networks) {
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生错误bla():
mrvn@frosties:~% g++ -O2 -W -Wall -g -std=gnu++17 -c bla.cc
In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,
from /usr/include/c++/5/bits/allocator.h:46,
from /usr/include/c++/5/vector:61,
from bla.cc:1:
/usr/include/c++/5/ext/new_allocator.h: In instantiation of ‘struct __gnu_cxx::new_allocator<const std::pair<std::__cxx11::basic_string<char>, int> >’:
/usr/include/c++/5/bits/allocator.h:92:11: required from ‘class std::allocator<const std::pair<std::__cxx11::basic_string<char>, int> >’
/usr/include/c++/5/bits/stl_vector.h:79:14: required from ‘struct std::_Vector_base<const std::pair<std::__cxx11::basic_string<char>, int>, std::allocator<const std::pair<std::__cxx11::basic_string<char>, int> > >::_Vector_impl’
/usr/include/c++/5/bits/stl_vector.h:164:20: required from ‘struct std::_Vector_base<const std::pair<std::__cxx11::basic_string<char>, int>, std::allocator<const std::pair<std::__cxx11::basic_string<char>, int> > >’
/usr/include/c++/5/bits/stl_vector.h:214:11: required from ‘class std::vector<const std::pair<std::__cxx11::basic_string<char>, int> >’
bla.cc:10:17: required from here
/usr/include/c++/5/ext/new_allocator.h:93:7: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const std::pair<std::__cxx11::basic_string<char>, int>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::pair<std::__cxx11::basic_string<char>, int>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::pair<std::__cxx11::basic_string<char>, int>&]’ cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^
/usr/include/c++/5/ext/new_allocator.h:89:7: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const std::pair<std::__cxx11::basic_string<char>, int>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::pair<std::__cxx11::basic_string<char>, int>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::pair<std::__cxx11::basic_string<char>, int>&]’
address(reference __x) const _GLIBCXX_NOEXCEPT
^
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么?
注意:使用g ++ 5.4和7.3.
这是我迄今为止从标准和文档中收集到的内容:
std::vector是一个分配器感知容器.
根据C++ 17(最终工作草案N4659)
20.5.3.5分配器要求
[allocator.requirements]表30说:
T,U,C任何cv不合格的对象类型(6.9)
因为std::vector它还要求元素类型是完整类型并且满足Erasable的要求.
从[container.requirements.general]/15我们有:
给定一个分配器类型A并给定一个容器类型X,其value_type与T相同,allocator_-类型与给
allocator_traits<A>::rebind_alloc<T>定一个m类型的左值相同 ,类型A的指针p,类型T*的表达式v(可能是const)T和rv类型的右值T,定义了以下术语.
......
(15.6) - T是E的可擦除意味着以下表达式格式正确:allocator_traits<A>::destroy(m, p)
由于问题中的元素类型是const合格的,因此无法满足要求.