这段代码可以干净地编译,并且可以与我尝试过的所有编译器一起使用,除了GCC 8(和当前的GCC干线):
std::make_shared<volatile int>(0)
我想知道:
std::atomic
,但是语义并不相同,因此我不是在寻找使用它代替volatile
的建议。答案 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)...);
}