const变量的static_assert [英] static_assert of const Variable

查看:304
本文介绍了const变量的static_assert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

  const float foo = 5.0F; 

static_assert(foo> 0.0F,foo必须大于0);

但是在 visual-studio-2010 我收到错误消息:


错误C2057:预期的常量表达式

我实际上正确地做了这件事, visual-studio-2010 没有正确实现 static_assert ,对吗?在 visual-studio-2017 它按预期工作。






有一些关于 const constexpr 。我明白这个区别,但是很多编译器都支持 static_assert 的使用,所以我会再问一次,这是不是合法的代码?我并不担心哪种编译器支持它,我担心它是否在C ++标准下定义。

foo> 0.0F 不是
核心常量表达式



  1. 表达式e是核心常量表达式,除非e抽象机器的规则将评估以下表达式中的一个


    $ b

    ...



    (2.7)一个左值到右值的转换,除非它适用于

    (2.7.1)一个非易失性glvalue整数或枚举类型,
    指的是一个完整的非易失性常量对象,其中前面的
    初始化,用常量表达式初始化,或者

    (2.7.2)引用字符串
    的子对象的非易失性glvalue,或

    (2.7.3)非易失性glvalue它是指用constexpr定义的非易失性对象
    ,或者是指
    的不可变子对象,例如对象或

    (2.7.4)一个非易失性的文字类型的glvalue,指的是一个
    非易失性对象,其生命周期始于评估e;

foo 是浮点类型,对于 foo> 0.0F 需要对 foo 进行左值到右值转换,这与上述条件不匹配,则 foo> 0.0F 不被视为常量表达式 :b
$ b


常量表达式是一个glvalue核心常量表达式,它引用一个实体,该实体是一个常量表达式的允许结果或者是一个prvalue核心常量表达式,其值满足以下约束:

另一方面,如果声明 foo 作为整数类型的代码将罚款。 LIVE (使用 constexpr 而不是 const 也可以。 LIVE


I have this code:

const float foo = 5.0F;

static_assert(foo > 0.0F, "foo must be greater than 0.");

But in I get the error:

error C2057: expected constant expression

I'm actually doing this correctly and just hasn't properly implemented static_assert, right? In it works as intended.


There has been some commentary of the differences between const and constexpr. I understand this difference, however many compilers support this use of static_assert so I'll ask again, is this legal code or not? I'm not as concerned about which compiler supports it, I'm concerned about whether it's defined under the C++ standard.

解决方案

foo > 0.0F is not a core constant expression:

  1. An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:

...

(2.7) an lvalue-to-rvalue conversion unless it is applied to

(2.7.1) a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or

(2.7.2) a non-volatile glvalue that refers to a subobject of a string literal, or

(2.7.3) a non-volatile glvalue that refers to a non-volatile object defined with constexpr, or that refers to a non-mutable subobject of such an object, or

(2.7.4) a non-volatile glvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of e;

foo is of floating-point type, for foo > 0.0F an lvalue-to-rvalue conversion on foo is required, which doesn't match the above conditions, then foo > 0.0F is not considered as constant expression:

A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints

On the other hand, if declare foo as integral type the code would be fine. LIVE (Usingconstexpr instead of const works too. LIVE)

这篇关于const变量的static_assert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆