gpt4 book ai didi

c++ - std::array-like 类复制构造函数

转载 作者:行者123 更新时间:2023-11-30 01:09:01 25 4
gpt4 key购买 nike

我正在为 GPU 编写代码,所以我不能使用 STL 类,因为方法需要特殊注释才能在 GPU 上运行。因此,我正在重新实现 std::array,并在其方法上添加适当的注释 (INLINE)。我现在遇到的问题是我的复制构造函数调用成员上的赋值运算符:

template <typename T, Int n>
class Few {
T array_[n];

public:
INLINE Few(Few<T, n> const& rhs) {
for (Int i = 0; i < n; ++i) array_[i] = rhs.array_[i];
}
};

这对简单的 T 来说没问题,但有一种类型需要调用复制构造函数,而不是赋值运算符。如何让编译器复制构造数组中的每个元素?

最佳答案

How do I get the compiler to copy construct each element in the array ?

它已经会了。这正是默认复制构造函数在这里所做的。所以不要自己提供一个,或者明确默认它:

Few(Few const& ) = default;

关于c++ - std::array-like 类复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961701/

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