
下面的代码基于 C++17,msvc 是最新版本的( 16.8.2 ),gcc/clang 也是最新版本的,可在线运行:godblot
namespace user { template<typenae 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
1 wutiantong OP 顺带提一下,这样修改可以使 msvc 的输出符合预期: inline const auto & decimal_data_unit = unit<decimal_data>; 主要想讨论一下,上面的写法是否有问题,还是编译器的 bug ? |
2 lcdtyph 2020-11-25 16:14:11 +08:00 static constexpr value_type unit_val() noexcept { return 10; } |
3 wutiantong OP @lcdtyph 确实是 work 的,而且我发现问题还跟 data 的构造函数有关: 如果给 data 上一个普通的构造函数,结果又不行了(变成 0 ),需要再进一步把构造函数修饰成 constexpr (这也说明例子中原本默认的 aggregate 构造函数是 constexpr,但我好像没找到标准说明?) 所以问题是,这个行为到底是 msvc 比较正确还是 gcc 比较正确呢? |
4 lcdtyph 2020-11-25 17:29:10 +08:00 via iPhone |
5 Wirbelwind 2020-11-26 13:27:32 +08:00 |