【发布时间】:2019-02-15 13:01:26
【问题描述】:
这段代码编译干净,可以与我尝试过的所有编译器一起使用,除了 GCC 8(和当前的 GCC 主干):
std::make_shared<volatile int>(0)
我想知道:
- GCC 8 拒绝此代码是否正确?
- 是否有 GCC 8 可以接受的替代品(具有相同的语义和性能)?我知道
std::atomic,但语义不一样,所以使用它而不是volatile的建议不是我想要的。
【问题讨论】:
-
澄清一点:你知道
volatile不会给你任何线程安全,也不保证不会有优化? -
I am aware of std::atomic, but the semantics are not the same so suggestions to use it instead of volatile are not what I'm looking for.你在寻找什么语义?我会更容易为你找到替代品。 -
IDK 您的用例,但以下解决方法,即使 GCC 4.8 也接受,可能对您有用(添加
volatile是完全安全的,static_pointer_cast也适用):std::const_pointer_cast<volatile int>(std::make_shared<int>(0))跨度> -
@ArneVogel:感谢您的提示。这似乎工作正常:
shared_ptr<volatile int> p = make_shared<int>(0).
标签: c++ c++11 gcc volatile make-shared