As this answer said:
The component type of containers like vectors must be assignable. References are not assignable (you can only initialize them once when they are declared, and you cannot make them reference something else later). Other non-assignable types are also not allowed as components of containers, e.g. vector is not allowed.
class test {
public:
test(test &) = delete;
test &operator=(test &) = delete;
};
int main() {
std::vector<test> vec;
}
class test
is not assignable but this could compile.
Other answer like std::vector<T, std::allocator>'s allocator limitation, why those two static_assert
was wrote in vector but not allocator ? Is this the real reason that limited the component type?
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class vector : protected _Vector_base<_Tp, _Alloc>
{
#if __cplusplus >= 201103L
static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
"std::vector must have a non-const, non-volatile value_type");
# ifdef __STRICT_ANSI__
static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
"std::vector must have the same value_type as its allocator");
# endif
#endif
Why volatile is so special that cannot use as vector's component qualifier?
Just expecting to know why, not the solution to bypass this.