【问题标题】:Visual C++ find out max possible value of variable's data typeVisual C++找出变量数据类型的最大可能值
【发布时间】:2014-09-13 12:20:28
【问题描述】:

我正在使用 VS2010 将现有的用 C++ 编写的 Mac 应用程序移植到 Windows。以下代码行:

T var_max;
var_max = std::numeric_limits<typeof(var_max)>::max();

给定一个变量,确定该变量的数据类型的最大值。 如何在 VC++ 中编写等效代码?我尝试使用 typeid 来确定数据类型,但它只给出一个字符串,不能在模板中使用。

【问题讨论】:

    标签: visual-studio-2010 visual-c++ rtti


    【解决方案1】:

    您无需确定数据类型。是 T。试试这个代码:

    #include <limits>
    template <typename T>
    T MaxValue()
    {
        T var_max = (std::numeric_limits<T>::max)();
        return var_max;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int value = MaxValue<int>();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2019-01-02
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多