首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解密问题

解密问题
EN

Stack Overflow用户
提问于 2010-11-24 20:40:43
回答 3查看 530关注 0票数 0

几分钟前,我试着这样做:

代码语言:javascript
复制
 #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);
     }
    };

请查看代码中的注释(就在类名称下面)

EN

回答 3

Stack Overflow用户

发布于 2010-11-24 21:11:03

这只是一个半个答案(所以请随意给它投上半个赞成票),但谷歌带来了一个comp.std.c++线程(由litb发起),其中正在讨论以下代码:

代码语言:javascript
复制
template<int const I>
struct A
{
   decltype(I) m;
}; 

如果在此上下文中,非类型模板参数上的decltype是合法的,那么我相信它在您的上下文中也应该是合法的。

票数 1
EN

Stack Overflow用户

发布于 2010-11-24 20:47:27

正如您在模板中声明的那样,Mantissashort,不需要decltype

如果希望使用泛型类型,请使用:template < typename Type, Type Value >

票数 0
EN

Stack Overflow用户

发布于 2011-02-11 08:20:43

好吧,我把钱押在窃听器上。您没有提到您使用的是哪种编译器,但stdafx建议使用多种Visual Studio,而我没有。

规范规定,如果e是一个实体,则decltype(e)的类型应为e。实体是值、对象、引用、函数、枚举数、类型、类成员、模板、模板专门化、命名空间、参数包或this。在你的例子中,尾数是一个prvalue,因为它是一个非类模板参数,据我所知,它是一个值,因此是一个实体。因此,根据规范,它看起来应该可以工作。

我已经在另外两个编译器: CodeGear RAD Studio2010和g++ 4.3.4中测试了你的代码(做了一些不相关的小修改)。在RAD Studio中,它找不到正确的模板:我得到‘无法找到"max"’。如果我将一个“最大”枚举值添加到基类NumericLimits中,static_assert会找到该值并进行比较。

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

无论如何,你可以通过一个静态变量来“清洗”这个类型,如下所示:

代码语言:javascript
复制
    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_;

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4266886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档