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

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

问题描述

我有这个代码:

const float foo = 5.0F;static_assert(foo > 0.0F, "foo 必须大于 0.");

但是在 我得到错误:

<块引用>

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

我实际上这样做是正确的, 只是没有正确实现 static_assert,对吧?在 它按预期工作.

<小时>

对于 constconstexpr 之间的差异有一些评论.我理解这种差异,但是许多编译器支持这种使用 static_assert 所以我再问一遍,这是否合法代码?我不关心哪个编译器支持它,我关心它是否在C++标准下定义.

解决方案

foo >0.0F 不是核心常量表达式:p><块引用>

  1. 表达式 e 是核心常量表达式,除非根据抽象机的规则对 e 的求值会求值以下表达式:

...

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

(2.7.1) 整数或枚举类型的非易失性左值,它指的是一个完整的非易失性 const 对象,前面有一个初始化,用常量表达式初始化,或

(2.7.2) 引用字符串子对象的非易失性泛左值字面意思,或

(2.7.3) 引用非易失性对象的非易失性泛左值用 constexpr 定义,或者指的是一个不可变的子对象这样的对象,或

(2.7.4) 文字类型的非易失性左值,它引用非易失性对象,其生命周期始于对 e 的评估;

foo 是浮点类型,对于 foo >0.0F 需要对foo进行左值到右值的转换,不符合上述条件,则foo >;0.0F 不被视为常量表达式:p><块引用>

常量表达式要么是泛指值核心常量表达式,它引用作为常量表达式(如下定义)的允许结果的实体,要么是值满足以下约束的纯右值核心常量表达式

另一方面,如果将 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天全站免登陆