variant<int, string> 可以是 constexpr 吗?
Can variant<int, string> be constexpr?
来自cppreference documentation for variant::variant():
This constructor is constexpr if and only if the value initialization of the alternative type T_0 would satisfy the requirements for a constexpr function.
上面没有提到变体中的其他类型,所以如果变体必须有一个非平凡的析构函数,那么声明 constexpr variant<int, string> v;
是否有效?
您的引用并不意味着声明 constexpr variant<int, string> v;
有效。引用 N4140(大致是 C++14 而不是 C++1z,但概念没有改变):
7.1.5 The constexpr specifier [dcl.constexpr]
9 A constexpr
specifier used in an object declaration declares the object as const
. Such an object shall have literal type and shall be initialized. If it is initialized by a constructor call, that call shall be a constant expression (5.19). [...]
仅仅因为构造函数调用是常量表达式,声明是无效的,类型也需要是文字类型。文字类型的要求之一是它们具有平凡的析构函数。
来自cppreference documentation for variant::variant():
This constructor is constexpr if and only if the value initialization of the alternative type T_0 would satisfy the requirements for a constexpr function.
上面没有提到变体中的其他类型,所以如果变体必须有一个非平凡的析构函数,那么声明 constexpr variant<int, string> v;
是否有效?
您的引用并不意味着声明 constexpr variant<int, string> v;
有效。引用 N4140(大致是 C++14 而不是 C++1z,但概念没有改变):
7.1.5 The constexpr specifier [dcl.constexpr]
9 A
constexpr
specifier used in an object declaration declares the object asconst
. Such an object shall have literal type and shall be initialized. If it is initialized by a constructor call, that call shall be a constant expression (5.19). [...]
仅仅因为构造函数调用是常量表达式,声明是无效的,类型也需要是文字类型。文字类型的要求之一是它们具有平凡的析构函数。