在enabled-if类中析构函数的脱行定义
out of line definition for destructor in enabled-if class?
如何为带有std::enable_if参数的模板类编写行外析构函数体?
(我需要它来编写一个装饰器,为其他对象类型添加一个标识符)。
代码:template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* = nullptr>
class IdentifiedInstance: public T
{
public:
virtual ~IdentifiedInstance() = 0; // abstract base, still needs a function body
};
身体定义:template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* = nullptr>
IdentifiedInstance::~IdentifiedInstance()
{
}
不能编译,因为IdentifiedInstance::
应该是IdentifiedInstance<T, ???>::
。
这里的第二个参数值是什么?我该怎么写呢?
笔记(1):
Visual Studio 2015接受以下格式:
template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* = nullptr>
class IdentifiedInstance: public T
{
public:
~IdentifiedInstance() = 0
{
}
};
不幸的是,我写的代码也需要在linux下构建(并且gcc正确地阻塞了这种形式)。
注释(2):我试着在网上搜索这个,但是我发现的大多数答案都是关于如何部分专门化析构函数。
这个怎么样?
template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* S>
IdentifiedInstance<T,S>::~IdentifiedInstance()
{
}
只命名第二个参数。
我还删除了默认值,因为您可能不会两次给出默认值(声明和定义)。
相关文章:
- 添加自定义析构函数时,Move 构造函数在派生类中消失
- 在从仅移动类型派生的类中定义析构函数在使用 std::vector emplace_back或push_back创建时会
- 为什么在定义析构函数时隐式删除移动构造函数
- 仅在删除包含对象的向量时调用自定义»析构函数«
- 这个递归类需要一个自定义析构函数?
- C++ - 定义自定义析构函数时程序崩溃
- 定义析构函数可以防止成员函数内联
- 使用自定义析构函数时出现"Undefined reference"错误
- C++ 在迭代器类中定义析构函数
- `不可复制`与自定义析构函数
- 是否可以使用 std::shared_ptr 创建共享对象池,并在没有自定义析构函数的情况下创建weak_ptr
- 我们什么时候必须在派生类 c++ 中定义析构函数
- 为什么当定义析构函数时,编译器不再添加默认的move函数和赋值函数
- 为标量类型显式定义析构函数
- 提供(空的)用户定义析构函数会导致编译错误
- 为什么带有用户定义析构函数的类的实例将成员指针设置为零
- 如何正确定义析构函数
- 带有Placement New的自定义析构函数
- 什么时候需要定义析构函数?
- 是否可以重新定义析构函数