C++11/C++14 中的文字类型 Class

Literal Type Class in C++11/C++14

int x = 1; // Not Constant

class A {
    public:
        int value  = x;
        int value2 { x };
        A( int a )        : value( x ), value2( x ) {}
        A( int a, int b ) : value{ x }, value2{ x } {}
        constexpr A() : value{ 0 }, value2{ 0 } {}
};

constexpr int function( A obj1, A obj2, A obj3, A obj4, A obj5, A obj6, A obj7 ){ 
    return 1; 
}

int main(){

    int y = 2; // Not Constant
    A obj1   ( y );
    A obj2   { y };
    A obj3 =   y  ;
    A obj4 = { y };
    A obj5   ( y, y );
    A obj6   { y, y );
    A obj7 = { y, y };
    int var = function( obj1, obj2, obj3, obj4, obj5, obj6, obj7 );

    return 0;
}

C++11 标准 (ISO/IEC 14882:2011),第 3.9 节,第 10 段规定(强调我的):

A type is a literal type if it is:

  • a scalar type; or
  • a reference type; or
  • a class type (Clause 9) that has all of the following properties:
    • it has a trivial destructor,
    • every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression (5.19),
    • it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
    • it has all non-static data members and base classes of literal types; or
  • an array of literal type.

在我看来,考虑到粗体项目符号,class A 不是 C++11 中的文字类型,因为有构造函数调用和非静态的大括号或相等初始化器不是常量表达式的数据成员。我尝试将 constexpr 放在构造函数定义之前,并将构造函数调用分配给 constexpr 变量以检查编译器是否确实抱怨,因为它们不是常量表达式。但是,Clang 和 GCC 都成功编译了它。所以我可能错了。

C++14 中删除了粗体项目符号 (N3652),因此我理解 class A 是 C++14 中的文字类型。我需要知道,因为 functionconstexpr,因此 each of its parameter types shall be a literal type(C++11/C++14 标准,第 7.1.15 节,第 3 段)。

编辑: 在原文中 Post 我使用了一个简单的例子来让它更容易阅读,并解释说我已经尝试了很多组合。现在我用其中一些组合更新了示例,以显示我尝试了不同的构造函数调用、定义和非静态数据成员初始化。谢谢。

Does anyone know why class A is a literal type?

由于您在 post 中引用的原因,它不在 C++11 中,但在 C++14 中。然而...

However, both Clang and GCC compile it successfully. So I am probably wrong.

你没有看错。 clang 和 gcc 都不是。尽管 A 不是文字类型,但代码确实是格式正确的……只是因为没有什么要求它是文字类型。 function 可能是一个 constexpr 函数,但它不是作为常量表达式调用的。 var 不是 constexpr 对象,因此没有强制要求所有对象都是文字类型。你试过了吗:

constexpr int var = function(...);

then 代码格式不正确,因为在这种情况下,function(...) 需要是核心常量表达式,这需要文字类型,即C++11 A 中的要求失败了。 (实际上,您在 C++14 中也会遇到 A 中的 none 是 constexpr 对象的问题。