有没有办法在 c++20 中创建不可复制的聚合结构?

gre*_*g_p 4 c++ aggregate c++20

在 C++20 中,聚合不能有用户声明或继承的构造函数(因此您不能将它们声明为删除)。那么有没有办法让结构体仍然是聚合的,但不可复制呢?

Bar*_*rry 6

添加不可复制的基数是可行的,但它的缺点是基数是第一个,因此它会与 paren 初始化混淆。做完全相同的想法,但把它放在最后更好:

struct noncopyable {
    noncopyable() = default;
    noncopyable(noncopyable&&) = default;
    noncopyable& operator=(noncopyable&&) = default;
};

struct foo {
    int i;
    // ... more members here ...
    [[no_unique_address]] noncopyable _ = {};
};
Run Code Online (Sandbox Code Playgroud)

从聚合中删除任何用户声明的构造函数的论文是P1008。非常不幸的是,我们基本上绕了一圈 - 在 C++03 中,为了使类不可复制,您将复制构造函数声明为私有。在 C++11 中,我们可以显式删除它,这使得代码中发生的情况更加清晰。在 C++20 中,我们取消了对聚合执行此操作的能力,因此现在我们又回到了奇怪的恶作剧。