例子:
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
}
谢谢!
最佳答案
你不能。使用特定于编译器的扩展或 Boost 的 Typeof(将特定于编译器的行为隐藏在一致的接口(interface)后面)。
在 C++0x 中,你可以使用 decltype
:decltype(ip) iv = 3;
如果你的编译器支持 C++0x 的这个方面,你很幸运。
关于c++ - 如何获取变量的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672597/