在constexpr构造函数中复制数组

无名

我用constexpr复制构造函数编写了一个类(在示例中,它是一种使结构更简单的结构。)字段之一是数组。我也想复制它。

struct Foo
{
    static constexpr int SIZE = 4;
    constexpr Foo() = default;
    constexpr Foo(const Foo &foo) :
            arr{foo.arr[0], foo.arr[1], foo.arr[2], foo.arr[3]},
            bar(foo.bar+1) {}
    int arr[SIZE] = {0, 0, 0, 0};
    int bar = 0;
};

我的版本可以使用,但是无法扩展。如果更改SIZE,则必须修改构造函数。另外,代码看起来很难看。

有没有更好的方法在构造函数中复制数组?构造函数必须是constexpr

托哈瓦

您可以使用std :: array。由于它是聚合类型,因此我相信这会起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章