I saw this answer: Does C++11 allow vector<const T>
? explains how you should not use const T
in a std::vector
. I tried this with std::vector<const int> int_vector
and got compiler error, which is expected. But if I create an std::vector<const CustomType*> custom_type_vector
I can use it with no problem. Does this mean c++ allows const pointer within a std::vector
but does not allow const T
in std::vector
?
The error log for using std::vector<const int>
is:
/usr/include/c++/7/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 int; __gnu_cxx::new_allocator<_Tp>::const_pointer = const int*; __gnu_cxx::new_allocator<_Tp>::const_reference = const int&]' cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
^~~~~~~
/usr/include/c++/7/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 int; __gnu_cxx::new_allocator<_Tp>::pointer = const int*; __gnu_cxx::new_allocator<_Tp>::reference = const int&]'
address(reference __x) const _GLIBCXX_NOEXCEPT
^~~~~~~
/usr/include/c++/7/ext/new_allocator.h:125:19: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]
::operator delete(__p);
~~~~~~~~~~~~~~~~~^~~~~
My compiler version is gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
Aucun commentaire:
Enregistrer un commentaire