获取对象的类型

The*_* do 2 c++ typeinfo

我试图沿着这些方向做点什么:

int var = 5;
std::numeric_limits<typeid(var)>::max();
Run Code Online (Sandbox Code Playgroud)

但惊讶的是,它不起作用.我怎样才能解决这个问题?
谢谢.

Jam*_*lis 11

您可以使用以下类型:

int the_max = std::numeric_limits<int>::max()
Run Code Online (Sandbox Code Playgroud)

您可以使用辅助函数模板:

template <typename T>
T type_max(T)
{
    return std::numeric_limits<T>::max();
}

// use:
int x = 0;
int the_max = type_max(x);
Run Code Online (Sandbox Code Playgroud)

在C++ 0x中,您可以使用decltype:

int x = 0;
int the_max = std::numeric_limits<decltype(x)>::max();
Run Code Online (Sandbox Code Playgroud)