const 传播到 std::array 个指针
const propagation to std::array of pointers
为什么 std::array
的数据类型在这里被不同地实例化
using T = const int *;
std::array<T, 4> x = { &a, &b, &c, &d }; // name: class std::array<int const *,4>
x[0] = &c; // OK : non-constant pointer
*x[0] = c; // Error : constant data
和这里相比?
using T = int *;
std::array<const T, 4> x = { &a, &b, &c, &d }; // name: class std::array<int * const,4>
x[0] = &c; // Error : constant pointer
*x[0] = c; // OK : non-constant data
第二种情况等同于const std::array<T, 4>
(指向非常量数据的常量指针)。
如果我们直接使用 const int *
:std::array<const int*, 4>
我们会得到第一种情况行为。
更准确地说,为什么 using T = int*; std::array<const T, 4>;
等同于 std::array<int*const, 4>
而不是 std::array<const int*, 4>
?
why is using T = int*; std::array<const T, 4>;
equivalent to std::array<int*const, 4>
and not std::array<const int*, 4>
?
因为 const
在 T
上是限定的,指针本身不是(也不可能)在指针对象上限定的。所以 const T
表示 const
指针,而不是指向 const
的指针。
不管T
是不是指针,规则都是一样的
using T = int; // const T => int const
using T = int*; // const T => int* const, not int const*
using T = int**; // const T => int** const, neither int* const*, nor int const**
注意第三个例子,如果const
在pointee上限定,const T
应该是int* const*
,或者它应该在pointee的pointee上限定,即int const**
?
using T = const int *; //T is a pointer to a CONSTANT integer.
std::array<T, 4> x = { &a, &b, &c, &d }; //array of PointerToConstantInteger, size=4
更改数组 x
的元素,但不要取消引用并尝试更改存储在其中的值。
using T = int *; //T is a pointer to an integer.
std::array<const T, 4> x = { &a, &b, &c, &d }; //array of CONSTANT IntegerPointer, size=4
无法更改数组 x
的元素,但取消引用和更改其中存储的值没有问题。
为什么 std::array
的数据类型在这里被不同地实例化
using T = const int *;
std::array<T, 4> x = { &a, &b, &c, &d }; // name: class std::array<int const *,4>
x[0] = &c; // OK : non-constant pointer
*x[0] = c; // Error : constant data
和这里相比?
using T = int *;
std::array<const T, 4> x = { &a, &b, &c, &d }; // name: class std::array<int * const,4>
x[0] = &c; // Error : constant pointer
*x[0] = c; // OK : non-constant data
第二种情况等同于const std::array<T, 4>
(指向非常量数据的常量指针)。
如果我们直接使用 const int *
:std::array<const int*, 4>
我们会得到第一种情况行为。
更准确地说,为什么 using T = int*; std::array<const T, 4>;
等同于 std::array<int*const, 4>
而不是 std::array<const int*, 4>
?
why is
using T = int*; std::array<const T, 4>;
equivalent tostd::array<int*const, 4>
and notstd::array<const int*, 4>
?
因为 const
在 T
上是限定的,指针本身不是(也不可能)在指针对象上限定的。所以 const T
表示 const
指针,而不是指向 const
的指针。
不管T
是不是指针,规则都是一样的
using T = int; // const T => int const
using T = int*; // const T => int* const, not int const*
using T = int**; // const T => int** const, neither int* const*, nor int const**
注意第三个例子,如果const
在pointee上限定,const T
应该是int* const*
,或者它应该在pointee的pointee上限定,即int const**
?
using T = const int *; //T is a pointer to a CONSTANT integer.
std::array<T, 4> x = { &a, &b, &c, &d }; //array of PointerToConstantInteger, size=4
更改数组 x
的元素,但不要取消引用并尝试更改存储在其中的值。
using T = int *; //T is a pointer to an integer.
std::array<const T, 4> x = { &a, &b, &c, &d }; //array of CONSTANT IntegerPointer, size=4
无法更改数组 x
的元素,但取消引用和更改其中存储的值没有问题。