[英]const_iterator type of a container of const types
If I compile the code below: 如果我编译下面的代码:
#include <list>
using iter_t = std::list<const unsigned>::const_iterator;
int main(int, char**) {
return 0;
}
I get the following compile error: 我收到以下编译错误:
In file included from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/x86_64-apple-darwin13.4.0/bits/c++allocator.h:33:0,
from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:46,
from /usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/list:61,
from main.cpp:1:
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<const unsigned int>':
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:92:11: required from 'class std::allocator<const unsigned int>'
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:315:9: required from 'class std::_List_base<const unsigned int, std::allocator<const unsigned int> >'
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:447:11: required from 'class std::list<const unsigned int>'
main.cpp:3:41: required from here
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/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 unsigned int; __gnu_cxx::new_allocator<_Tp>::const_pointer = const unsigned int*; __gnu_cxx::new_allocator<_Tp>::const_reference = const unsigned int&]' cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/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 unsigned int; __gnu_cxx::new_allocator<_Tp>::pointer = const unsigned int*; __gnu_cxx::new_allocator<_Tp>::reference = const unsigned int&]'
address(reference __x) const _GLIBCXX_NOEXCEPT
^
I get roughly the same error message if I change the container type from std::list
to std::vector
, std::deque
or std::forward_list
. 如果将容器类型从
std::list
更改为std::vector
, std::deque
或std::forward_list
,我会得到大致相同的错误消息。 If I change the template argument from const unsigned
to unsigned
it compiles fine. 如果我将模板参数从
const unsigned
更改为unsigned
则可以正常编译。
What's going on here? 这里发生了什么? I can't see why it wouldn't compile.
我不明白为什么它不能编译。
Visual C++ 2015 gives the clarifying diagnostic Visual C ++ 2015提供了明确的诊断
” The C++ Standard forbids containers of
const
elements becauseallocator<const T>
is ill-formed.” C ++标准禁止使用
const
元素容器,因为allocator<const T>
格式不正确。
As I understand it, this is because an allocator's allocate
method returns a pointer to an array of uninitialized T
items, and if T
is const
then these items can never be initialized. 据我了解,这是因为分配器的
allocate
方法返回了指向未初始化的T
项目数组的指针,并且如果T
为const
则这些项目将永远无法初始化。
I can't find any explicit statement that allocator<const T>
is ill-formed, so it is presumably a consequence of the semantics. 我找不到任何明确声明
allocator<const T>
格式不正确,因此大概是语义的结果。
As Dieter Lücking notes in a comment , there is a direct clash for const
type T
between the two member functions that provide address of a reference
and const_reference
, because these are equal when the type is const
. 正如DieterLücking 在评论中指出的那样 ,在提供
reference
地址和const_reference
的两个成员函数之间, const
类型T
存在直接冲突,因为当类型为const
时,它们相等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.