0
votes

Is there a way to create your own enable_if like condition?

I have a template class: Foo<int, int, int> and I have another template class which takes a type: Template <T> class Bar.

I'd like to constrain the Bar class such that the type it takes may only be any of the Foo class specialisations. Eg, can take Foo<0, 0 ,1> but can't take type int.

Is there a short and neat way of requiring this constraint through a user-defined enable_if? Is there a better method I haven't considered? Ideally the solution would be relatively compact and clear, possibly in the way std::is_arithmetic works.

Thanks for your time and suggestions.

1

1 Answers

1
votes

No need to pull out any library types. The easiest way would be a good old specialization:

template <typename> class Bar;

template<int a, int b, int c>
class Bar<Foo<a, b, c>> {
  // Define `Bar`
};

Instantiating Bar with a type that is a Foo<...> would choose the specialization, while instantiating it with any other type will hit the dead end that is the incomplete class declaration of Bar<T>.