I have a type like this:
template<typename T>
struct wrapper
{
using foo = typename T::foo;
using bar = typename T::bar;
using baz = typename T::baz;
// More of those...
};
I would like foo
, bar
, baz
and equivalent type aliases to be defined if and only if the equivalent type exists in T
. Solutions using std::conditional
allow to replace it by something else when it doesn't exist, but I don't know how to make sure that it doesn't exist at all when the corresponding type doesn't exist in the template type. The code above causes an error when wrapper<T>
is instantiated if T
doesn't define one of the type aliases.
I can't make wrapper
inherit from T
because wrapper
isn't supposed to do everything T
can do. Also, using partial specialization would lead to some kind of exponential explosion and would quickly become unmaintainable. I could probably make foo
, bar
... template type aliases to inject an std::enable_if
in a default template parameter but then users would have to write wrapper<T>::foo<>
, wrapper<T>::bar<>
instead of wrapper<T>::foo
, wrapper<T>::bar
, etc... and I don't want that.
Is there a simple yet maintainable way to define such a type alias only when the corresponding type alias exists in T
?