我正在尝试编写一个有范围的指针类,该类一旦删除便会调用delete。我意识到我需要检查我的指针是否指向数组,因此可以调用正确的删除方法。从std :: unique_ptr汲取灵感,我使用type_traits检查保存类型指针的模板参数是否为数组:
template <typename type, bool _Dx = std::is_array<type>::value>
class scoped_ptr {
private:
type* m_ptr;
//...
};
template <typename type>
class scoped_ptr<type, true> {};
但是如果我的模板参数类型是“ int []”,则代码将无效,因为我无法拥有“ int [] * m_ptr”我怎么解决这个问题?我如何传递int []参数并使用“ int * m_ptr”
您想要的是std::remove_extent
。您可以使用std::remove_extent
在它和std::conditional
之间切换,例如
std::conditional
还请注意,type
是非法名称。所有以下划线开头并以大写字母开头的名称都保留用于实现。