C++ – Template class methods definition with enable_if as template parameter

c++templates

I asked this question earlier where a solution was presented. The solution is great as far as the question is concerned, but now I am confused on how I would define the methods outside of the class i.e. I would like to define the methods in an .inl file. What would be the syntax in this case?

Just to be clear, for a template class, the method definition will be:

template <typename T>
struct Foo
{
  Foo();
};

// C-tor definition
template <typename T>
Foo<T>::Foo()
{
}

How would I define methods for the template class with enable_if as one of the parameters?

template <typename Policy, enable_if< is_base<BasePolicy, Policy>::value >::type >
struct Foo
{
  Foo();
};

// C-tor definition -- ???

Best Solution

From the looks of it, you want to do something along the lines of this:

template <typename Policy,
          typename = typename std::enable_if<std::is_base_of<BasePolicy, Policy>::value>::type >
struct Foo;

template <typename Policy>
struct Foo<Policy> {
    Foo();
};

template <typename Policy>
Foo<Policy>::Foo() {
}

This sneakily takes advantage of the default argument in a few places: don't get confused, there is an implicit void sitting in several locations.

Related Question