Jump to content

  • Log In with Google      Sign In   
  • Create Account

Conditional default wrapper template parameter.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 tanzanite7   Members   -  Reputation: 1294

Like
0Likes
Like

Posted 27 February 2014 - 03:30 AM

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, 27 February 2014 - 03:39 AM.


Sponsor:

#2 tanzanite7   Members   -  Reputation: 1294

Like
0Likes
Like

Posted 27 February 2014 - 04:38 AM

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, 27 February 2014 - 04:45 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS