wayne 发表于 2018-10-11 12:39:21

inline constexpr auto SIGMA( std::size_t n ) noexcept
{
    return std::begin( { 0.0, 0.3085375, 0.6914625, 0.9331928, 0.9937903, 0.9997674, 0.9999966 } )[ n ];
}

我不确定你同事的这段代码是否肯定能在 编译期计算出来。因为std::begin好像不是constant expression。

https://en.cppreference.com/w/cpp/language/constant_expression

gxqcn 发表于 2018-10-11 13:27:23

试过了,可以的:std::begin, std::cbegin,标准库对std::begin(std::initializer_list) 进行了特化。

可以用 static_assert 调用到其值,说明是在编译期可计算出来的。
页: 1 [2]
查看完整版本: 如何优雅地获取常量数列中某个值?