gpt4 book ai didi

具有数组类型的 C++ add_pointer 模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:13:47 24 4
gpt4 key购买 nike

我想使用 std::add_pointer<type>来自 C++ 的模板 <type_traits> header 构造一个指向数组类型的指针。

但是,下面会产生错误

double *y[2];
std::add_pointer<double[2]>::type x;
y = x; // generates error

我的 MSVC C++ 编译器附带 SDK 7.1 说

error C2440: '=' : cannot convert from 'double (*)[2]' to 'double *[2]' There are no conversions to array types, although there are conversions to references or pointers to arrays

我在这里错过了什么?

编辑:我希望 y 是一个二维数组,一维为 2,另一个为变量。预期用途是

y[i][0]y[i][1]

最佳答案

yx 在这里没有相同的类型。

如错误消息所述,y 是一个元素类型为 double* 的数组。 x 是指向类型为 double[2] 的数组的指针。

您可以将 y 的类型更改为:

double (*y)[2];
std::add_pointer<double[2]>::type x = new double[2][2];
y = x;
// use y[0][0], y[0][1], y[1][0] ...
delete[] y;

LIVE

关于具有数组类型的 C++ add_pointer 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com