Search code examples
c++boostboost-mpl

how to get type of variable?


example:

template<typename T>
struct type_of {
   typedef boost::mpl::if_<boost::is_pointer<T>,
   typename boost::remove_pointer<T>::type,
   T
   >::type type;
};

int main() {
   int* ip;
   type_of<ip>::type iv = 3; // error: 'ip' cannot appear in a constant-expression
}

Thanks!


Solution

  • You cannot. Either use compiler-specific extensions or Boost's Typeof (which hides the compiler-specific behavior behind a consistent interface).

    In C++0x, you may use decltype: decltype(ip) iv = 3; If your compiler supports this aspect of C++0x, you're in luck.