How does one share common_fn() among all specializations (for Widget<A<T> > and Widget<B<T> >, no matter what T is) in the code below?
#include <cassert>
struct Afoo {};
struct Bfoo {};
template<typename T> struct A { typedef Afoo Foo; };
template<typename T> struct B { typedef Bfoo Foo; };
template<typename Type> struct Widget
{
Widget() {}
typename Type::Foo common_fn() { return Type::Foo(); }
int uncommon_fn() { return 1; }
};
template<typename T> struct Widget<A<T> >
{
Widget() {}
int uncommon_fn() { return 2; }
};
int main()
{
Widget<A<char> > WidgetAChar;
assert( WidgetAChar.common_fn() == Afoo() ); // Error
assert( WidgetAChar.uncommon_fn() == 2 );
}
I had tried earlier to simplify the question to what I thought was its essence, but it turns out that it is necessary to ask it in the context of partial specialization and traits.
It’s a little unclear what you’re aiming for, in particular whether
uncommon_fnis really as simple as illustrated, or might be more.But anyway, for the example code given, consider …
Generalizing this to handle a more general
uncommon_fnshouldn’t be hard.You might also consider the inheritance trick that @iammilind showed for your previous question. It might be practically simpler. However, it adds the possibility of accessing a possibly “wrong” function implementation.
Cheers & hth.