I have a situation where I would like to define a specialization to be the same as the instantiation of another class. Here's a simple example of what I want (a full intantiation in this example; in my actual problem, I want a partial specialization):
//Template class
template <int i> class Foo { /*...*/ };
//Another, completely different, template class
template <int i,int j> class Bar { /*...*/ };
//Ordinary specialization of Foo
template <> class Foo<0> { /*...*/ };
//Bogus specializations of Foo that
// hopefully explain what I want.
template <> class Foo<1> = Bar<1, 11>;
template <> class Foo<2> = Bar<2, 42>;
template <> class Foo<3> = Bar<3,-71>;
...
What would be the advantage of doing this? The definitions of Foo<1>
, Foo<2>
, etc. are fairly complicated, but easily written once as a template. There are a lot of such definitions. Working the definition of Bar
into Foo
isn't an option. Only some values can be specialized, and the specialization of Bar
must be hand-chosen (hence the random numbers for int j
).
I would ordinarily achieve this effect by using CRTP. I would make some fairly nasty modifications to Bar
, and then do something like:
template <> class Foo<1> : public Bar<1, 11,Foo<1>> {};
template <> class Foo<2> : public Bar<2, 42,Foo<2>> {};
template <> class Foo<3> : public Bar<3,-71,Foo<3>> {};
...
Obviously, Bar
would need to be altered, with maybe some using
declarations to pull the constructors down. This would be messy.
My question: can I do better than this? That is, can a specialization be defined to be an instantiation of another template class?
Note: preferred standard is C++14, although later versions would be acceptable.