C++ 常量类型容器的常量迭代器类型

C++ 常量类型容器的常量迭代器类型,c++,stl,containers,C++,Stl,Containers,如果我编译下面的代码: #include <list> using iter_t = std::list<const unsigned>::const_iterator; int main(int, char**) { return 0; } #包括 使用iter\u t=std::list::const\u迭代器; int main(int,char**){ 返回0; } 我得到以下编译错误: In file included from /usr/local

如果我编译下面的代码:

#include <list>

using iter_t = std::list<const unsigned>::const_iterator;

int main(int, char**) {
  return 0;
}
#包括
使用iter\u t=std::list::const\u迭代器;
int main(int,char**){
返回0;
}
我得到以下编译错误:

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
   ^
在/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/x86_64-apple-darwin13.4.0/bits/c++分配器中包含的文件中。h:33:0,
来自/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:46,
从/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/list:61,
来自main.cpp:1:
/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h:在“class\uu gnu\u cxx::new_allocator”的实例化中:
/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/bits/allocator.h:92:11:从'class std::allocator'中需要
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:315:9:从“类标准::_list_base”中需要
/usr/local/Cellar/gcc/4.9.2_1/include/c++/4.9.2/bits/stl_list.h:447:11:必须来自“类标准::列表”
main.cpp:3:41:从这里开始需要
/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h:93:7:错误:“constTp*gnucxx::new分配器::address(gnucxx::new分配器::constconst分配器::constconstconst参考)const[带无符号int;新分配器::constconst指针=const无符号分配器constuconst参考]不能超载
地址(常量引用)常量GLIBCXX\u无例外
^
/usr/local/ceral/gcc/4.9.2_1/include/c++/4.9.2/ext/new_allocator.h:89:7:错误:带有“_-Tp*u-gnu-cxx::new_分配器::address(u-gnu-cxx::new_分配器::reference)const[带u-Tp=const unsigned int;u-gnu-cxx::new_u分配器::pointer=const unsigned int*u-gnu分配器::指针=const unsigned int*u-cxx::new_u分配器::reference=unsigned=]
地址(参考)常数GLIBCXX无例外
^
如果我将容器类型从
std::list
更改为
std::vector
std::deque
std::forward\u list
,则会收到大致相同的错误消息。如果我将模板参数从
const unsigned
更改为
unsigned
,则编译效果良好


这是怎么回事?我不明白为什么它不能编译。

< P> Visual C++ 2015给出了明确的诊断<

<强> C++标准禁止容器的代码> const 元素,因为分配器< /C> >格式不正确。

据我所知,这是因为分配器的
allocate
方法返回指向未初始化
T
项数组的指针,如果
T
const
,则这些项永远无法初始化

我找不到任何明确的语句表明
allocator
格式不正确,所以这可能是语义的结果


作为Dieter L注释,在提供<代码>引用< <代码> >代码> const引用< /COD>的两个成员函数之间存在“<代码> const <代码>类型>代码> t>代码冲突,因为当类型为<代码> const 时,它们是相等的。

< P> Visual C++ 2015给出了明确的诊断

<强> C++标准禁止容器的代码> const 元素,因为分配器< /C> >格式不正确。

据我所知,这是因为分配器的
allocate
方法返回指向未初始化
T
项数组的指针,如果
T
const
,则这些项永远无法初始化

我找不到任何明确的语句表明
allocator
格式不正确,所以这可能是语义的结果


作为Dieter L的注释,在提供代码< >代码>引用/ <代码>和<代码> const引用两个成员函数之间,对于<代码> const <代码>类型>代码> t>代码>存在直接冲突,因为当类型为<代码> const 时,它们是相等的。我发现了。冲突是引用和const_引用的两个成员函数

address
(如果类型为常量,则两者相等)@DieterLücking:谢谢!更新。。。完成了。Visual C++可能是某事:我发现了。冲突是引用和const_引用的两个成员函数
address
(如果类型为常量,则两者相等)@DieterLücking:谢谢!更新。。。完成。