Advertisement Jump to content
Sign in to follow this  
tanzanite7

Conditional default wrapper template parameter.

This topic is 1786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!