我看到了这段代码
float range[] = { 0, 256 };
const float* histRange = { range };
对我而言,{ range }
表示“数组的数组”,因为变量 range
是一个浮点数数组。因此,对我来说,这更有意义。
const float** histRange = { range };
但编译器(来自VS2019的VC ++)不能接受它(期望一个const float*)。
有人能解释一下为什么{range}
不是'数组的数组'(float **),而只是普通的浮点指针吗?
有趣的事实:当我执行
float range[] = { 0, 256 };
auto aa = { range };
auto bb = range;
std::cout << typeid(aa).name() << std::endl;
std::cout << typeid(bb).name() << std::endl;
输出结果为(在VC++ VS2019中)。
std::initializer_list<float * __ptr64>
float * __ptr64
float* range = { 0, 256 };
,但这个可以:const float* histRange[] = { range };
。原因是{ 0, 256 }
和{ range }
不是你可以指向的数组,它们是准备初始化数组的值列表。严谨地说,std::initializer_list<T>
不能像真正的T[]
那样衰减为T*
。 - Ben Voigtstd::initializer_list
,除非在某些情况下,而这不是其中之一。 - M.M