【问题标题】:static_assert placement problemstatic_assert 放置问题
【发布时间】:2011-04-07 04:49:01
【问题描述】:

拥有(不用担心这段代码的长度,专注于 struct X 和 Range)(您可以复制粘贴并编译):
已编辑

    #include <limits.h>
#include <type_traits>
//This is from file "Static_limits.h"
template<class T>struct static_numeric_limits;
template<>struct static_numeric_limits<signed char>
{
    enum {min = SCHAR_MIN,max = SCHAR_MAX};
};
/*This "surplus" template is here for the reason that char is threated diferently from signed char */
template<>struct static_numeric_limits<char>
{
    enum {min = SCHAR_MIN,max = SCHAR_MAX};
};
template<>struct static_numeric_limits<unsigned char>
{
    enum {min = 0x0,max = UCHAR_MAX};
};
template<>struct static_numeric_limits<unsigned short>
{
    enum {min = 0x0,max = USHRT_MAX};
};
template<>struct static_numeric_limits<signed short>
{
    enum {min = SHRT_MIN,max = SHRT_MAX};
};
template<>struct static_numeric_limits<unsigned int>
{
    enum {min = 0x0,max = UINT_MAX};
};
template<>struct static_numeric_limits<signed int>
{
    enum {min = INT_MIN,max = INT_MAX};
};
template<>struct static_numeric_limits<unsigned long>
{
    enum {min = 0x0,max = ULONG_MAX};
};
template<>struct static_numeric_limits<signed long>
{
    enum {min = LONG_MIN,max = LONG_MAX};
};
template<>struct static_numeric_limits<unsigned long long>
{
    static const long long min = 0x0;
    static const long long max = ULLONG_MAX;
};
template<>struct static_numeric_limits<signed long long>
{
#define LLONG_MAX     9223372036854775807LL
    /* maximum signed long long int value */
    static const long long min = LLONG_MIN;
    static const long long max = LLONG_MAX;
};
//This is from main.cpp
typedef unsigned long long uint_64;
typedef signed long long int_64;
/*Validates range*/
template<class IntType, uint_64 value_,bool C = std::is_signed<IntType>::value>
struct validate_range;
template<class IntType,uint_64 value_>
struct validate_range<IntType,value_,true>
{
    enum {value = (static_cast<int_64>(value_) >= static_numeric_limits<IntType>::min) &&
                  (static_cast<int_64>(value_) <= static_numeric_limits<IntType>::max)
         };
};
template<class IntType,uint_64 value_>
struct validate_range<IntType,value_,false>
{
    enum {value = (value_ >= static_numeric_limits<IntType>::min) &&
                  (value_ <= static_numeric_limits<IntType>::max)
         };
};
template<class IntType, IntType value>
struct Range
{
private:
    const IntType value_;
protected:
    const IntType getRange()const
    {
        return value_;
}        public:
    Range():value_(value)
    {
        /*eb*/
    }
    //this static assert in here won't work even though this class is a base class for Low
    static_assert((validate_range<IntType, value>::value),"Value constant is out of range");
};
template<class IntType, IntType value>
struct Low : private Range<IntType,value>//HERE Range IS INHERITED BY Low
{
    const IntType getLowRange()const
    {
        return Range<IntType,value>::getRange();
    }
};
template<typename IntType, uint_64 low_range>
struct X : public Low<IntType,low_range>
{};
    //static_assert((validate_range<IntType, Value>::value),"Value constant is out of range");//this static doesn't work if placed in Low's base class namely Range        };
    int main(int argc, char** argv)
    {
        X<unsigned char, -2> x4;//this should fail
        return 0;
    }

所以基本上我在这里要问的是为什么 static_assert((validate_range::value),... 如果放在 Range 结构中不起作用,而 Range 结构又是 Low 的基类,而后者又是基类X 但是如果直接放在 struct X 中就可以了 已编辑
伙计们,我对以前的代码感到非常抱歉。我已经编辑了我的帖子,这次它应该可以工作(暴露真正的问题)。再一次,对不起最后的代码,我只是不知道发生了什么。

【问题讨论】:

  • 当你说它“不起作用”时,会发生什么?
  • 您至少需要在顶部添加一个#include &lt;climits&gt; 才能编译它。我仍然无法让它在 g++ 上编译(使用-std=c++0x)。 std::is_signed&lt;T&gt; 应该是std::numeric_limits&lt;T&gt;::is_signedlow_range 被引用但没有在任何地方定义。
  • 有几个错别字(可能?或者缺少代码,比如low_range),如果你用简单的英语陈述你想要实现的目标,它可能会有所帮助。

标签: c++ metaprogramming


【解决方案1】:

r这是我得到的错误。它们似乎很容易解释:

test.cpp:115:45: error: use of undeclared identifier 'Value'; did you mean 'value'?
     static_assert((validate_range<IntType, Value>::value),"Value constant is out of range");
                                            ^~~~~
                                            value
test.cpp:99:37: note: 'value' declared here
    template<class IntType, IntType value>
                                    ^
test.cpp:127:39: error: use of undeclared identifier 'low_range'
        struct X : public Low<IntType,low_range>
                                      ^
test.cpp:128:9: error: expected class name
        {
        ^
3 errors generated.

编辑

您的最新帖子为我编译(除了关于重新定义 LLONG_MAX 的警告)。

【讨论】:

  • 哇,令人印象深刻的错误信息——“你的意思是‘价值’吗?”。 :-) 我猜是叮当声。
  • 你是对的:clang。该团队已将重点放在质量错误消息上,它真的显示了! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 2016-07-19
  • 2023-04-09
  • 1970-01-01
  • 2020-02-27
相关资源
最近更新 更多