0

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 Answer 1

1

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>.

Sign up to request clarification or add additional context in comments.

2 Comments

(Sorry switched devices and can’t find other account) Thanks for the suggestion, I had thought this would require calling the class via Bar<0,0, 1> rather than the preferred Bar<Foo<0, 0, 1>>
@user7119460 - No, that's not how specialization works. It still expects a Foo.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.