以下声明在声明3.8.1中失败,但似乎在其他测试编译器(例如gcc 6.1,MSVC 2015,clang 3.9.1)中没有错误地编译。constexpr实例的初始化(例如`std :: integral_constant`)是否需要`= {}`?
constexpr std::integral_constant<int,0> myConstant;
铛3.8.1给出:
error: default initialization of an object of const type 'const std::integral_constant<int, 0>' without a user-provided default constructor constexpr std::integral_constant<int,0> myConstant;
而下面的测试中所有的编译器编译正确:
constexpr std::integral_constant<int,0> myConstant = {};
这到底是怎么回事? (是铿锵3.8.1错误是否正确?)
如果我定义我自己的类型,我应该写一个用户提供的默认ctor,以便用户可以避免键入={}
?
'std :: integral_constant'没有值表示(它是一个空的结构体)。你的“你应该总是明显地初始化”规则是否仍然适用? –
@RossBencina:“* std :: integral_constant没有值表示*”标准很清楚,所有'constexpr'变量都应该被初始化。它是否具有价值表现并不是标准关心的问题。在constexpr函数中声明的任何变量也是如此。 –
这个答案目前没有解决我的问题“这是怎么回事?” - 为什么不同编译器之间的行为不同?哪个编译器是正确的? –