对于std::array,我找不到这样做的方法,但是内置数组可以正常工作。
GCC 8.2.0规定如下:
#include <iostream>
#include <array>
int main()
{
const volatile std::array<int,2> v = {1,2};
std::cout << v[0] << std::endl ;
}
<source>: In function 'int main()':
<source>:6:21: error: passing 'const volatile std::array<int, 2>' as
'this' argument discards qualifiers [-fpermissive]
std::cout << v[0] << std::endl ;
^
In file included from <source>:2:
/opt/compiler-explorer/gcc-8.2.0/include/c++/8.2.0/array:185:7: note:
in call to 'constexpr std::array<_Tp, _Nm>::value_type& std::array<_Tp,
_Nm>::operator[](std::array<_Tp, _Nm>::size_type) [with _Tp = int; long
unsigned int _Nm = 2; std::array<_Tp, _Nm>::reference = int&;
std::array<_Tp, _Nm>::value_type = int; std::array<_Tp, _Nm>::size_type =
long unsigned int]'
operator[](size_type __n) noexcept
^~~~~~~~
Compiler returned: 1
虽然
#include <iostream>
int main()
{
const volatile int v[2] = {1,2};
std::cout << v[0] << std::endl ;
}
很好用。
如何访问const volatile std::array?