c – 据我所知,下面的函数不是constexpr,但是代码在clang和g中编译.我失踪了什么

c – 据我所知,下面的函数不是constexpr,但是代码在clang和g中编译.我失踪了什么,第1张

概述我在N4140的§5.19/ 2中得到了这个例子: constexpr int incr(int &n) { return ++n;} 据我所知,这不是一个constexpr函数.但是这段代码在俚语和g中编译.见live example.我在这里缺少什么? 在C14中,constexpr函数的规则放松了,文章 N3597: Relaxing constraints on constexp 我在N4140的§5.19/ 2中得到了这个例子:
constexpr int incr(int &n) {    return ++n;}

据我所知,这不是一个constexpr函数.但是这段代码在俚语和g中编译.见live example.我在这里缺少什么?

解决方法 在C14中,constexpr函数的规则放松了,文章 N3597: Relaxing constraints on constexpr functions.本文介绍了理论和效果,包括以下(重点是):

As in C++11,the constexpr keyword is used to mark functions which the implementation is required to evaluate during translation,if they are used from a context where a constant Expression is required. Any valID C++ code is permitted in constexpr functions,including the creation and modification of local variables,and almost all statements,with the restriction that it must be possible for a constexpr function to be used from within a constant Expression. A constant Expression may still have sIDe-effects which are local to the evaluation and its result.

和:

A handful of syntactic restrictions on constexpr functions are
retained:

asm-declarations are not permitted. try-blocks and function-try-blocks are not permitted. Declarations of variables with static and thread storage duration have some restrictions (see below).

我们可以在N4140第7.1.5节[dcl.constexpr]中找到这个内容:

The deFinition of a constexpr function shall satisfy the following constraints:

it shall not be virtual (10.3);

its return type shall be a literal type;

each of its parameter types shall be a literal type;

its function-body shall be = delete,= default,or a compound-statement that does not contain

an asm-deFinition,

a goto statement,

a try-block,or

a deFinition of a variable of non-literal type or of static or thread storage duration or for which
no initialization is performed.

最后一个例子显示了如何在constexpr中使用incr:

constexpr int h(int k) {  int x = incr(k); // OK: incr(k) is not required to be a core                   // constant Expression  return x;}constexpr int y = h(1); // OK: initializes y with the value 2                        // h(1) is a core constant Expression because                        // the lifetime of k begins insIDe h(1)

而涵盖k的寿命的规则从h(1)开始就是:

modification of an object (5.17,5.2.6,5.3.2) unless it is applIEd to a non-volatile lvalue of literal type
that refers to a non-volatile object whose lifetime began within the evaluation of e;

7.1.5 [dcl.constexpr]中的措词告诉我们为什么incr是一个有效的constexpr:

For a non-template,non-defaulted constexpr function or a non-template,non-defaulted,non-inheriting
constexpr constructor,if no argument values exist such that an invocation of the function or constructor
Could be an evaluated subExpression of a core constant Expression (5.19),the program is ill-formed; no
diagnostic required.

作为T.C的修改示例:

constexpr int& as_lvalue(int&& i){ return i; }constexpr int x = incr(as_lvalue(1)) ;

显示,我们确实可以使用incr作为核心常数表达式的子表达式,因此它不是不成形的.

总结

以上是内存溢出为你收集整理的c – 据我所知,下面的函数不是constexpr,但是代码在clang和g中编译.我失踪了什么全部内容,希望文章能够帮你解决c – 据我所知,下面的函数不是constexpr,但是代码在clang和g中编译.我失踪了什么所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1233245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存