GCC 8无法编译make_shared <volatile int =“”>()

时间:2018-09-11 04:53:53

标签: c++ c++11 gcc volatile make-shared

这段代码可以干净地编译,并且可以与我尝试过的所有编译器一起使用,除了GCC 8(和当前的GCC干线):

std::make_shared<volatile int>(0)

我想知道:

  1. GCC 8是否正确拒绝此密码?
  2. GCC 8是否可以接受替代品(具有相同的语义和性能)?我知道std::atomic,但是语义并不相同,因此我不是在寻找使用它代替volatile的建议。

在此处查看:https://godbolt.org/z/rKy3od

1 个答案:

答案 0 :(得分:5)

根据标准语言,这是libstdc ++不符合。

这可能是一个错误。 make_shared使用标准分配器allocate_shared调用std::allocator<remove_const_t<T>>,其中T是共享对象的类型。该分配器仅用于获取基础共享对象(包含volatile int和atomic计数器的结构)的重新绑定的分配器。因此,最好将此基础对象声明为非const非易失性。

make_shared的定义将起作用:

template<class T,class...Args>
auto make_shared(Args&&...args){
    using ncvT= std::remove_cv_t<T>;
    return std::allocate_shared<T>(std::allocator<ncvT>(),std::forward<Args>(args)...);
}