4

来自N4140 §5.19/2.3(强调我的)

— 调用未定义的constexpr 函数或 未定义的constexpr 构造函数;

从 §7.1.5/2 开始,constexpr 函数和构造函数被隐式内联,也就是说,如果 constexpr 函数未在 TU 中定义,则代码将无法编译。

4

1 回答 1

7

此项目符号由缺陷报告 699添加,它要求必须在使用前定义 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

请注意,该报告中的措辞随着缺陷报告 1365更改为当前标准草案中的措辞。

于 2014-12-29T13:55:14.093 回答