Ill-Formed, No Diagnostic Required (NDR): ConstExpr Function Throw in C++14

后端 未结 1 861
醉梦人生
醉梦人生 2021-01-20 06:43
#include 
using namespace std;

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

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


        
1条回答
  •  有刺的猬
    2021-01-20 07:36

    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.

    or is it incorrect (and there is a bug in both Clang and 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.

    0 讨论(0)
提交回复
热议问题