在其他容器中使用 boost::container::static_vector 时 gcc 编译错误 "binding ‘const s’ to reference of type ‘s&’ discards qualifiers"
gcc compile error "binding ‘const s’ to reference of type ‘s&’ discards qualifiers" when using boost::container::static_vector in other container
在另一个容器(例如 [=23=)中使用 boost::container::static_vector 时,gcc 返回编译错误。以下测试用例重现了错误:
#include <boost/container/static_vector.hpp>
#include <vector>
struct s
{
boost::container::static_vector<int,6> a;
};
int main()
{
std::vector<s> b;
b.resize(6);
}
在 Ubuntu Xenial 上遵守 g++ template_test2.cpp
导致以下错误:
In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
from /usr/include/c++/5/string:40,
from /usr/include/c++/5/stdexcept:39,
from /usr/include/boost/container/throw_exception.hpp:26,
from /usr/include/boost/container/new_allocator.hpp:24,
from /usr/include/boost/container/vector.hpp:28,
from /usr/include/boost/container/static_vector.hpp:25,
from template_test2.cpp:1:
/usr/include/c++/5/bits/stl_algobase.h: In instantiation of ‘typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type std::__fill_a(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s; typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type = void]’:
/usr/include/c++/5/bits/stl_algobase.h:747:20: required from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s]’
/usr/include/c++/5/bits/vector.tcc:469:14: required from ‘void std::vector<_Tp, _Alloc>::_M_fill_insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:1073:23: required from ‘void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:716:10: required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type, std::vector<_Tp, _Alloc>::value_type) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
template_test2.cpp:12:11: required from here
/usr/include/c++/5/bits/stl_algobase.h:701:11: error: binding ‘const s’ to reference of type ‘s&’ discards qualifiers
*__first = __value;
仅当 struct s
包含静态矢量时才会出现此错误。此错误的原因是什么,是否有可用的解决方法?
看起来编译器生成的默认复制赋值运算符有签名
s& operator= (s&);
并且您的代码在这行 fill
算法中失败(在 resize
方法中以某种方式调用):
for (; __first != __last; ++__first)
*__first = __value; // <----- call operator=(s&) passing const s& object
因为 __value
是对 s
对象的 const 引用,它不能作为参数传递给
operator=(s&)
(const对象不可修改)
关于为 类 生成默认复制赋值运算符,您可以阅读 here。 static_vector
的实现有些奇怪,默认复制赋值运算符将 s&
作为参数而不是 const s&
。
要解决此问题,您可以实施您自己的此运算符版本:
s& operator = (const s& other) {
a = other.a;
return *this;
}
在另一个容器(例如 [=23=)中使用 boost::container::static_vector 时,gcc 返回编译错误。以下测试用例重现了错误:
#include <boost/container/static_vector.hpp>
#include <vector>
struct s
{
boost::container::static_vector<int,6> a;
};
int main()
{
std::vector<s> b;
b.resize(6);
}
在 Ubuntu Xenial 上遵守 g++ template_test2.cpp
导致以下错误:
In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
from /usr/include/c++/5/string:40,
from /usr/include/c++/5/stdexcept:39,
from /usr/include/boost/container/throw_exception.hpp:26,
from /usr/include/boost/container/new_allocator.hpp:24,
from /usr/include/boost/container/vector.hpp:28,
from /usr/include/boost/container/static_vector.hpp:25,
from template_test2.cpp:1:
/usr/include/c++/5/bits/stl_algobase.h: In instantiation of ‘typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type std::__fill_a(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s; typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type = void]’:
/usr/include/c++/5/bits/stl_algobase.h:747:20: required from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s]’
/usr/include/c++/5/bits/vector.tcc:469:14: required from ‘void std::vector<_Tp, _Alloc>::_M_fill_insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:1073:23: required from ‘void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:716:10: required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type, std::vector<_Tp, _Alloc>::value_type) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
template_test2.cpp:12:11: required from here
/usr/include/c++/5/bits/stl_algobase.h:701:11: error: binding ‘const s’ to reference of type ‘s&’ discards qualifiers
*__first = __value;
仅当 struct s
包含静态矢量时才会出现此错误。此错误的原因是什么,是否有可用的解决方法?
看起来编译器生成的默认复制赋值运算符有签名
s& operator= (s&);
并且您的代码在这行 fill
算法中失败(在 resize
方法中以某种方式调用):
for (; __first != __last; ++__first)
*__first = __value; // <----- call operator=(s&) passing const s& object
因为 __value
是对 s
对象的 const 引用,它不能作为参数传递给
operator=(s&)
(const对象不可修改)
关于为 类 生成默认复制赋值运算符,您可以阅读 here。 static_vector
的实现有些奇怪,默认复制赋值运算符将 s&
作为参数而不是 const s&
。
要解决此问题,您可以实施您自己的此运算符版本:
s& operator = (const s& other) {
a = other.a;
return *this;
}