Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 解型问题_C++_Decltype - Fatal编程技术网

C++ 解型问题

C++ 解型问题,c++,decltype,C++,Decltype,几分钟前,我尝试过这样做: #include "stdafx.h" #include <iostream> #include <cmath>//for pow #include <limits> #include <limits.h> using std::cout; template<class T> struct NumericLimits; template<

几分钟前,我尝试过这样做:

 #include "stdafx.h"
    #include <iostream>
    #include <cmath>//for pow
    #include <limits>
    #include <limits.h>
    using std::cout;
    template<class T>
    struct NumericLimits;
    template<>
    struct NumericLimits<short>
    {
     enum :short {max = SHRT_MAX};
    };

    template<short Mantissa, short Exponent, short Base = 10>
    class AllocFactorScientific
    {

     static_assert(Mantissa > 0,"Mantissa component MUST be > zero");
//why if this works:
     static_assert(Mantissa < NumericLimits<short>::max, "Provided number is too large.");
//this doesn't: 
static_assert(Mantissa < NumericLimits<decltype(Mantissa)>::max, "Provided number is too large.");
    private:
     long double factor_; 
     long double calcFactor_(long int mantissa,long int exponent)
     {
      return mantissa * ::pow(Base,exponent);
     }
    public:
     AllocFactorScientific():factor_(getFactor()){}
    static const long double getFactor()
     {
      cout << "decltype(Mantissa): " << typeid(decltype(Mantissa)).name() << '\n';
      return Mantissa * ::pow(static_cast<double>(Base),Exponent);
     }

     void setFactor(long int mantissa,long int exponent)
     {
      factor_ = calcFactor_(mantissa,exponent);
     }
    };
#包括“stdafx.h”
#包括
#包括//战俘
#包括
#包括
使用std::cout;
模板
结构数值极限;
模板
结构数值极限
{
枚举:short{max=SHRT_max};
};
模板
类AllocFactorScientific
{
静态断言(尾数>0,“尾数分量必须>零”);
//为什么这样做有效:
静态断言(尾数cout
尾数
短的
,正如您在模板中声明的,不需要
decltype

如果需要泛型类型,请使用:
模板

这只是半个答案(请随意投票),但谷歌提出了一个线程(由litb发起),其中讨论了以下代码:

template<int const I>
struct A
{
   decltype(I) m;
}; 
模板
结构A
{
decltype(I)m;
}; 

如果在这种情况下,
decltype
在非类型模板参数上是合法的,我相信它在您的模板参数上应该是合法的。

好吧,我的钱在一个bug上。您没有提到您使用的编译器,但是stdafx建议使用各种Visual Studio,而我没有

说明书上说decltype(e)如果e是实体,则应具有e的类型。实体是值、对象、引用、函数、枚举数、类型、类成员、模板、模板专门化、命名空间、参数包或此。在您的示例中,尾数是prvalue,因为它是非类模板参数,我可以说它是值,因此是en所以,根据规范,它似乎应该可以工作

我已经在另外两个编译器中尝试了您的代码(进行了一些小的、不相关的修改):CodeGear RAD Studio 2010和g++4.3.4。在RAD Studio中,它找不到正确的模板:我得到“找不到“max”。如果我将“max”枚举值添加到基本NumericLimits类中,静态断言会找到该值并进行比较

在g++中,我得到了内部编译器错误

值得一提的是,您可以通过如下静态变量“清洗”类型:

    template<short Mantissa, short Exponent, short Base = 10>
    class AllocFactorScientific
    {
    static decltype(Mantissa) MantissaLaundry;

     static_assert(Mantissa > 0,"Mantissa component MUST be > zero");
//why if this works:
     static_assert(Mantissa < NumericLimits<short>::max, "Provided number is too large.");
//this works as well now:
static_assert(Mantissa < NumericLimits<decltype(MantissaLaundry)>::max, "Provided number is too large.");
    private:
     long double factor_;
模板
类AllocFactorScientific
{
静态decltype(尾数)尾数;
静态断言(尾数>0,“尾数分量必须>零”);
//为什么这样做有效:
静态断言(尾数

这在RAD Studio中似乎有效,在Visual Studio中也可能有效。或者,您可以缩小问题范围并提交错误报告。

@Alex抱歉,更新…您的尝试成功了吗?顺便说一句,您不必定义自己的
数值限制
,在
模板中已经有了
std::numeric_limits
我是为你做这件事的…我知道没有必要去打字。我只是想知道为什么这不起作用?