constexpr默认的默认构造函数 [英] constexpr defaulted default constructors
本文介绍了constexpr默认的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想将我的 default
-ed默认构造函数声明为 constexpr
,则Clang 3.8和GCC 5.3会出现编译器错误.根据此 stackoverflow问题,它应该可以正常工作:
I get compiler error by Clang 3.8 and GCC 5.3 if I want to declare my default
-ed default constructors as constexpr
. According to this stackoverflow question it just should work fine:
struct A
{
constexpr A() = default;
int x;
};
但是:
Error: defaulted definition of default constructor is not constexpr
您是否知道实际发生了什么?
Have you got any clue what is actually going on?
推荐答案
按现状,x仍未初始化,因此无法在编译时构造该对象.
As it stands, x remains uninitialized, so the object can not be constructed at compile time.
您需要初始化x:
struct A
{
constexpr A() = default;
int x = 1;
};
这篇关于constexpr默认的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文