views:

82

answers:

3

I'm sure that boost has some functions for doing this, but I don't know the relevant libraries well enough. I have a template class, which is pretty basic, except for one twist where I need to define a conditional type. Here is the psuedo code for what I want

struct PlaceHolder {};
    template <typename T>
class C{
    typedef (T == PlaceHolder ? void : T) usefulType;
};

How do I write that type conditional?

+2  A: 
template < typename T >
struct my_mfun : boost::mpl::if_
<
  boost::is_same<T,PlaceHolder>
, void
, T
> {};

template < typename T >
struct C { typedef typename my_mfun<T>::type usefulType; };
Noah Roberts
Code should be prefixed by 4 spaces. You can select text and click the 101010 button to do it in bulk.
GMan
Thanks, but please don't alter my answers.
Noah Roberts
@Noah: See the FAQ: "If you are not comfortable with the idea of your questions and answers being edited by other trusted users, this may not be the site for you."
Bill
+5  A: 

I think this is the principle you're after:

template< class T >
struct DefineMyTpe
{
  typedef T usefulType;
};

template<>
struct DefineMyType< PlaceHolder >
{
  typedef void usefulType;
};

template< class T > 
class C
{
  typedef typename DefineMyType< T >::usefulType usefulType;
};
stijn
+1  A: 

Also with the new standard:

typedef typename std::conditional<std::is_same<T, PlaceHolder>::value, void, T>::type usefulType

rafak