utnapistim utnapistim - 2 months ago 23
C++ Question

out of line definition for destructor in enabled-if class?

How do I write the out-of-line destructor body, for a template class with an std::enable_if argument?

(I need this to write a decorator, adding an identifier to other object types).

Code:

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
};


Body definition:

template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* = nullptr>
IdentifiedInstance::~IdentifiedInstance()
{
}


This doesn't compile because
IdentifiedInstance::
should be
IdentifiedInstance<T, ???>::
.

What is the second parameter value here?
How do I correctly write this?


Notes (1):

Visual Studio 2015 accepts this form:

template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* = nullptr>
class IdentifiedInstance: public T
{
public:
~IdentifiedInstance() = 0
{
}
};


Unfortunately the code I write needs to also be built under linux (and gcc rightfully chokes on this form).

Notes (2): I tried searching for this online, but most answers I found, referred to how to partially specialize the destructor only.

Answer

What about this?

template<typename T,
typename std::enable_if<std::is_base_of<X,T>::value>::type* S>
IdentifiedInstance<T,S>::~IdentifiedInstance()
{
}

Just naming the second parameter.

I also removed the default value, because you may not give default values twice (declaration and definition).

Comments