我认为自己对C ++的基本方面相当了解,但有时我会感到困惑。我试图实现一对作用于std::vector
实例的函数,并对其值进行一些操作,而不是就地更改它们。当然,我认为const&
的通行证是合理的。但是,以下MWE无法编译,我想了解原因:
#include<vector>
void func1(const std::vector<const char* const>& v){
// do stuff
}
void func2(const std::vector<const std::vector<const char* const> >& v){
for(int i=0;i<v.size();++i){
func1(v[i]);
}
}
int main(){
return 0;
}
错误消息看起来像这样:
In file included from...include/c++/4.9.3/x86_64-unknown-linux-gnu/bits/c++allocator.h:33:0,
from...include/c++/4.9.3/bits/allocator.h:46,
from...include/c++/4.9.3/vector:61,
from test.cxx:2:
.../ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const std::vector<const char* const> >’:
.../bits/allocator.h:92:11: required from ‘class std::allocator<const std::vector<const char* const> >’
.../ext/alloc_traits.h:172:53: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<const std::vector<const char* const> > >’
.../bits/stl_vector.h:75:28: required from ‘struct std::_Vector_base<const std::vector<const char* const>, std::allocator<const std::vector<const char* const> > >’
.../bits/stl_vector.h:214:11: required from ‘class std::vector<const std::vector<const char* const> >’
test.cxx:9:18: required from here
.../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::vector<const char* const>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::vector<const char* const>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::vector<const char* const>&]’ cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^
.../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::vector<const char* const>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::vector<const char* const>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::vector<const char* const>&]’
address(reference __x) const _GLIBCXX_NOEXCEPT
^
.../ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const char* const>’:
.../bits/allocator.h:92:11: required from ‘class std::allocator<const char* const>’
.../ext/alloc_traits.h:172:53: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<const char* const> >’
.../bits/stl_vector.h:75:28: required from ‘struct std::_Vector_base<const char* const, std::allocator<const char* const> >’
.../bits/stl_vector.h:214:11: required from ‘class std::vector<const char* const>’
test.cxx:10:14: required from here
.../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 char* const; __gnu_cxx::new_allocator<_Tp>::const_pointer = const char* const*; __gnu_cxx::new_allocator<_Tp>::const_reference = const char* const&]’ cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^
.../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 char* const; __gnu_cxx::new_allocator<_Tp>::pointer = const char* const*; __gnu_cxx::new_allocator<_Tp>::reference = const char* const&]’
address(reference __x) const _GLIBCXX_NOEXCEPT
我对错误消息声称某些内容无法重载这一事实感到困惑,指的是func1
内func2
的调用。似乎解决const
对向量条目的引用存在问题,但我真的不明白为什么这段代码无法编译。我很高兴任何有关此类行为的文档的解释,建议或指针。
答案 0 :(得分:3)
如果您事先知道这意味着什么,编译器会正确报告其问题。
std::allocator
有两个address
重载。
address(reference __x)
address(const_reference __x)
编译器的问题是,对于模板类型const T
,T&
和const T&
这两种类型的类型相同。然后它认为有两个相同的重载副本。
答案 1 :(得分:2)
const
您正尝试使用std::vector<const char*>
类型实例化矢量。类型必须至少可移动,而且不是。改为:
{{1}}
代替