c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?

标签 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) {
  }
}

这会在 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 lvalue m of type A, a pointer p of type T*, an expression v of type (possibly const) T, and an rvalue rv of type T, 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/

相关文章:

c++ - 无法将重复字段添加到 protobuf?

c++ - 如何匹配模板类中的模板友元函数

c++ - 一份拷贝如何使用 memcpy union 简单成员?

c - extern 对于内联函数声明是否重要?

c++ - `std::bind()` 标准库算法如何实现?

c++ - 在 Linux 上添加 POCO C++ 库 Netbeans

c++ - 使用stat和lstat检测与文件有关的错误

c - 已声明但从未定义的不透明结构

c++ - 访问 std::variant 中的公共(public)结构成员

c++ - GCC 7 C++ 17 对折叠表达式的支持