下面的代码基于 C++17,msvc 是最新版本的( 16.8.2 ),gcc/clang 也是最新版本的,可在线运行:godblot
namespace user
{
template<typename T> const T unit { 1 };
template<typename Def>
struct data
{
typename Def::value_type val;
};
template<typename Def> const data<Def> unit<data<Def>> { Def::unit_val() };
struct decimal_def
{
using value_type = int;
static value_type unit_val() noexcept { return 10; }
};
using decimal_data = data<decimal_def>;
inline const auto decimal_data_unit = unit<decimal_data>;
}
#include <iostream>
int main()
{
std::cout << user::decimal_data_unit.val << std::endl;
}
预期输出:10
msvc 输出:0
gcc/clang 输出:10
顺带提一下,这样修改可以使 msvc 的输出符合预期:
inline const auto & decimal_data_unit = unit<decimal_data>;
主要想讨论一下,上面的写法是否有问题,还是编译器的 bug ?
static constexpr value_type unit_val() noexcept { return 10; }
@lcdtyph 确实是 work 的,而且我发现问题还跟 data 的构造函数有关:
如果给 data 上一个普通的构造函数,结果又不行了(变成 0 ),需要再进一步把构造函数修饰成 constexpr (这也说明例子中原本默认的 aggregate 构造函数是 constexpr,但我好像没找到标准说明?)
所以问题是,这个行为到底是 msvc 比较正确还是 gcc 比较正确呢?
@wutiantong
我对标准了解的不深,说不上哪边是对的…我也没有 msvc 环境,上面的修改是根据经验改的
@wutiantong 不是构造函数的问题
在初始化的时候需要调用 unit_val(),问题是编译器 什么时候 用 unit_val()的返回值对它赋值