Sign in to follow this  
tanzanite7

Conditional default wrapper template parameter.

Recommended Posts

tanzanite7    1410
Let's say i have:
template<class T, template<class> class Wrap> struct Snafu;
I can use it as "Snafu<int, WrapZ>". Nice.

Now i would like Wrap to have a default value, conditionally:
template<class T> struct Type {
    typedef typename std::enable_if<std::is_funky<T>::value, WrapA<T>>::type type;
    ... etc
};

template<class T, class Wrap = Type<T>::type> struct Snafu;
Unfortunately, this will degrade it down to "Snafu<int, WrapB<int>>" instead of the previous, preferred "Snafu<int, WrapB>" (assuming for the sake of this specific example, that i happen to want to use WrapB instead of whatever it selected on its own).

Have been up for 24 hours ... and can not wrap my head around it. Can i have the cake and eat it too?

edit: Wait a minute. Actually, it seems i can not use typedef for substitution trickery as easily. I need sleep. Edited by tanzanite7

Share this post


Link to post
Share on other sites
tanzanite7    1410
I think i figured it out:
template<class T> struct WrapVoid { ... };
template<class T> class is_wrap_void { ... };

template<class T, template<class> class Wrap = WrapVoid> struct Snafu {
    typedef typename std::conditional<is_wrap_void<Wrap>::value, WrapZ<T>, Wrap<T>>::type Wrapper;

    // now use Wrapper instead of Wrap
};
Boilerplate ellipsis to be filler in when i wake up.

edit: also, Google, this thing is henceforth called "conditional default template template parameter".

edit2: erm, "is_wrap_void<Wrap>::value" == "std::is_same<Wrap<T>, WrapVoid<T>>::value". Edited by tanzanite7

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this