我有这个简单的代码:
#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) {
}
}
这会在 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
^
我的问题是:为什么?
注意:使用了 g++ 5.4 和 7.3。
最佳答案
这是到目前为止我可以从标准和文档中收集到的内容:
std::vector
是一个 allocator-aware容器。
根据 C++17(最终工作草案 N4659)
20.5.3.5 Allocator requirements
[allocator.requirements]
Table 30 says:
T, U, C any cv-unqualified object type (6.9)
对于 std::vector
还要求元素类型是完整类型,满足Erasable的要求。
来自 [container.requirements.general]/15
我们有:
Given an allocator type A and given a container type X having a value_type identical to T and an allocator_- type identical to
allocator_traits<A>::rebind_alloc<T>
and given an lvaluem
of typeA
, a pointerp
of typeT*
, an expressionv
of type (possibly const)T
, and an rvaluerv
of typeT
, the following terms are defined.
...
(15.6) — T is Erasable from X means that the following expression is well-formed:allocator_traits<A>::destroy(m, p)
由于问题中的元素类型是 const
合格,不符合要求。
关于c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54145084/