Conditional Type Definitions

I'm sure boost has some features for this, but I don't know the corresponding libraries well enough. I have a template class that is pretty simple, with the exception of one turn, 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 to write this conditional type?

+5
source share
3 answers

Also with the new standard:

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

+9
source

I think this is the principle you need:

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;
};
+6
source
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; };
+2
source

All Articles