格式错误,无需诊断(NDR):C ++ 14中抛出了ConstExpr函数 [英] Ill-Formed, No Diagnostic Required (NDR): ConstExpr Function Throw in C++14

查看:97
本文介绍了格式错误,无需诊断(NDR):C ++ 14中抛出了ConstExpr函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

constexpr int f(bool b){ return b ? throw 0 : 0; } // OK 

constexpr int f() { return f(true); } // Ill-Formed, No Diagnostic Required

int main(){

    try{
        f();
    }catch( int x ){
        cout << "x = " << x << endl;
    }

    return 0;
}

此代码是C ++ 14标准(ISO / IEC 14882:2014),第7.1.5节,第5段:

This code is an example from the C++14 Standard (ISO/IEC 14882:2014), Section 7.1.5, Paragraph 5:


用于非模板,非默认constexpr函数或非模板,非默认,非继承constexpr构造函数,如果不存在任何参数值,以致函数或构造函数的调用可以是核心常量表达式的评估子表达式(5.19),则程序格式错误;

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.

它被描述为 格式错误,不需要诊断,因为 throw-expression 不是核心常量表达式(5.19 / 2)。但是,Clang和GCC都成功编译了它( Ideone )。

It is described as "ill-formed, no diagnostic required" because a throw-expression is not a core constant expression (5.19/2). However, both Clang and GCC compile it successfully (Ideone).


  • 此代码正确(标准中有错误)还是不正确(Clang和GCC中都存在错误)?

我还发现了有关标准措辞的有趣讨论:

I also found these interesting discussions about the Standard wording:

  • Difference between Undefined Behavior and Ill-formed, no diagnostic message required
  • What is the rationale to "no diagnostic required"?
  • What is the C++ compiler required to do with ill-formed programs according to the Standard?

该程序是否可能是 <强>格式错误,不需要诊断,并且允许编译器成功编译?

Is it possible that a/this program is "ill-formed, no diagnostic required" and that the compilers are allowed to compile it successfully?

推荐答案


此代码是否正确(标准中有错误)

Is this code correct (and there is a mistake in the Standard)

由标准决定是否程序是正确的,即格式正确的。该标准明确指出该程序格式错误。

The standard is what decides if a program is "correct" i.e. well-formed. The standard explicitly says that the program is ill-formed.


还是不正确(Clang和GCC中都存在错误) ?

or is it incorrect (and there is a bug in both Clang and GCC)?

程序格式错误(错误)。 clang和gcc在观察到的行为上均符合标准。

The program is ill-formed (incorrect). Both clang and gcc are standard compliant in their observed behaviour.


该程序是否可能格式错误,没有诊断性并允许编译器成功编译?

Is it possible that a/this program is "ill-formed, no diagnostic required" and that the compilers are allowed to compile it successfully?

是。该标准不要求格式错误的程序必须无法编译。如果程序违反规则,则仅要求实现至少发出一条诊断消息。某些规则是不可诊断的,如果违反了这些规则,则不需要进行诊断。

Yes. The standard doesn't require that an ill-formed program must fail to compile. It merely requires that the implementation issues at least one diagnostic message, if the program violates the rules. Some rules are not diagnosable, and diagnostic is not required if such rules are violated.

实际上,该规则被认为是不可诊断的,因为对于编译器证明(通常)违反规则。

In fact, the rule is considered "not diagnosable" because it would be very difficult for the compiler to prove (in general) that the rule is violated. It is quite typical that "no diagnostic required" rule violations compile successfully.

如果格式错误的程序确实可以编译,则标准不会指定此类程序的行为方式。

If an ill-formed program does compile, the standard does not specify how such program should behave.

这篇关于格式错误,无需诊断(NDR):C ++ 14中抛出了ConstExpr函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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