GCC 8 无法编译 make_shared<volatile int>()
GCC 8 fails to compile make_shared<volatile int>()
这段代码编译干净,适用于我尝试过的所有编译器,除了 GCC 8(和当前的 GCC 主干):
std::make_shared<volatile int>(0)
我想知道:
- GCC 8 拒绝此代码是否正确?
- 是否有 GCC 8 会接受的替代品(具有相同的语义和性能)?我知道
std::atomic
,但语义不一样,所以使用它而不是 volatile
的建议不是我要找的。
根据标准语言,这是一个 libstdc++ 不符合项。
这可能是一个错误。 make_shared
使用标准分配器调用 allocate_shared
,std::allocator<remove_const_t<T>>
其中 T
是共享对象的类型。此分配器将仅用于为底层共享对象(包含 volatile int 和原子计数器的结构)获取重新绑定的分配器。所以将这个底层对象声明为非常量非易失性是完全没问题的。
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)...);
}
这段代码编译干净,适用于我尝试过的所有编译器,除了 GCC 8(和当前的 GCC 主干):
std::make_shared<volatile int>(0)
我想知道:
- GCC 8 拒绝此代码是否正确?
- 是否有 GCC 8 会接受的替代品(具有相同的语义和性能)?我知道
std::atomic
,但语义不一样,所以使用它而不是volatile
的建议不是我要找的。
根据标准语言,这是一个 libstdc++ 不符合项。
这可能是一个错误。 make_shared
使用标准分配器调用 allocate_shared
,std::allocator<remove_const_t<T>>
其中 T
是共享对象的类型。此分配器将仅用于为底层共享对象(包含 volatile int 和原子计数器的结构)获取重新绑定的分配器。所以将这个底层对象声明为非常量非易失性是完全没问题的。
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)...);
}