0

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!

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
niXman
  • 1,698
  • 3
  • 16
  • 40

2 Answers2

2

Within the current norm of C++, you can't get the type of variables, at least not without compiler-specific stuff (but try boost::typeof which gathers those tricks in a transparent way).

What you wrote is basically a template which removes a pointer qualifier from a type: type_of<int>::type is int as is type_of<int*>::type.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
2

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 1
    Thenks to all! I wrote test: http://liveworkspace.org/code/1925198987ec402e5f6ca589d7d4944d – niXman Sep 08 '10 at 23:56