tags:

views:

82

answers:

2

Hi all! 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
}

thenks!

+2  A: 

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.
+2  A: 

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.

GMan
Thenks to all! I wrote test: http://liveworkspace.org/code/1925198987ec402e5f6ca589d7d4944d
niXman