C++ 这些词是什么意思;“未定义”;以要点表示的平均值§;N4140中的5.19/2.3?

C++ 这些词是什么意思;“未定义”;以要点表示的平均值§;N4140中的5.19/2.3?,c++,language-lawyer,c++14,constexpr,constant-expression,C++,Language Lawyer,C++14,Constexpr,Constant Expression,从§5.19/2.3(强调矿山) -调用未定义的constepr函数或 未定义的constexpr构造函数 根据§7.1.5/2,constexpr函数和构造函数是隐式内联的,也就是说,如果TU中未定义constexpr函数,则代码将不会编译。此项目符号是由添加的,它要求在使用前必须定义constexpr函数或构造函数。缺陷报告在7.1.5中添加了以下示例以演示该规则: constexpr int square(int x); //OK, declaration constexpr

从§5.19/2.3(强调矿山)

-调用未定义的constepr函数或 未定义的constexpr构造函数

根据§7.1.5/2,constexpr函数和构造函数是隐式内联的,也就是说,如果TU中未定义constexpr函数,则代码将不会编译。

此项目符号是由添加的,它要求在使用前必须定义constexpr函数或构造函数。缺陷报告在
7.1.5
中添加了以下示例以演示该规则:

constexpr int square(int x);       //OK, declaration
constexpr struct pixel {           // error: pixel is a type
    int x;
    int y;
    constexpr pixel(int);            // OK, declaration
};
constexpr pixel::pixel(int a)
    : x(square(a)), y(square(a)) { } //OK, definition
constexpr pixel small(2);          // error: square not defined, so small(2)
                                     // not constant (5.19 [expr.const]), so constexpr not satisfied
constexpr int square(int x) {      // OK, definition
    return x * x;
}
constexpr pixel large(4);          // OK, square defined

注意,本报告中的措辞与标准草案中当前的措辞有所不同。

“代码将不会编译”这不是违反ODR吗?作为违反ODR的行为,这是UB,不需要诊断-但由于它是UB,我猜它违反了规则“常量表达式中没有UB”?我猜它指的是常数表达式之前的未定义,不允许这样的例子:@dyp我想你在coliru的例子中得到了答案。好吧,Shafik Yaghmour找到了证据,我只是猜测:)