在汇总初始化原子成员

发布于 2025-02-05 09:13:54 字数 1138 浏览 3 评论 0原文

似乎无法使用C ++ 14初始化原子成员。以下无效(在gcc 8.0.1.1 上播放

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{0, 1};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

error: use of deleted function 'std::atomic<int>::atomic(const std::atomic<int>&)'
   auto p = new stru{0, 1};
                     ^

) 原子类型既不可复制,也不是可移动的,因此不可复制。但是,以下似乎有效( live on GCC 8.0.1 )。

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

这有效地执行了零初始化,因此无法初始化到零以外的值。有什么方法可以初始化到其他指定的值?

There seems no way to initialize atomic members in aggregate using C++14. The following doesn't work (live on gcc 8.0.1):

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{0, 1};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

The error message:

error: use of deleted function 'std::atomic<int>::atomic(const std::atomic<int>&)'
   auto p = new stru{0, 1};
                     ^

This is because atomic types are neither copyable nor movable, and is thus not copy-initializable. The following seems to work however (live on gcc 8.0.1).

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

This effectively performs zero initialization, and is thus unable to initialize to values other than zero. Is there any way to initialize to other specified values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2025-02-12 09:13:54

帕特里克的解决方案有效,但他的解释对我来说并不正确。因此,我发布了我在这里找到的解释。对于代码auto p = new stru {{0},{1}}; 汇总初始化效果启动:

如果初始化器子句是嵌套的固定列表(不是
表达式),相应的数组元素/类成员/公共
基础(由于C ++ 17)
从该子句中列表:汇总
初始化是递归的。

因此,从Brad-Init列表中复制列表intialized,而不是复制初始化。

Patrick's solution works, but his explanation doesn't look right to me. So I post the explanation I find here. For the code auto p = new stru{{0}, {1}}; aggregate initialization kicks in with the effect:

If the initializer clause is a nested braced-init-list (which is not
an expression), the corresponding array element/class member/public
base (since C++17)
is list-initialized from that clause: aggregate
initialization is recursive.

As a result, instead of copy initialization, the members are copy list-initialized from the braced-init-list.

如何视而不见 2025-02-12 09:13:54

10不是std :: atomic_int32_t s。以下工作:

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{{0}, {1}};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

编辑:为什么会发生这种情况? 复制初始化启动初始化器列表中的每个元素

每个直接公共...非静态类成员,按照类别定义中的数组下标/外观顺序从初始化器列表的相应子句中进行了复制。

在您的示例中,10被隐式转换为std :: atomic_int32_t,以及std :: atomic_int32_t无法从std :: atomic_int32_t中复制进行复制化。在额外的{}版本中,很好。

1 and 0 aren't std::atomic_int32_ts. The following works:

#include <atomic>
#include <iostream>

struct stru {
  std::atomic_int32_t val_0;
  std::atomic_int32_t val_1;
};

int main() {
  auto p = new stru{{0}, {1}};
  std::cout << p->val_0 << ", " << p->val_1 << std::endl; 
}

EDIT: Why does this happen? Copy initialization kicks in for each element in the initializer list:

Each direct public...non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.

In your example, 1 and 0 are implicitly converted to std::atomic_int32_t, and a std::atomic_int32_t can't be copy-initialized from a std::atomic_int32_t. In the extra {} version, val_0 and val_1 are copy initialized from std::initializer_list<int>s, which is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文