【发布时间】:2021-03-18 21:48:28
【问题描述】:
我正在尝试使用 C++20 功能简洁地编写一个 constexpr 常量。
#include <utility>
template <template <typename T, T ... Ints> std::integer_sequence<T, Ints...> I>
static constexpr long pow10_helper = ((Ints, 10) * ...);
template <std::size_t exp>
static constexpr long pow10 = pow10_helper< std::make_index_sequence<exp> >;
static_assert(pow10<3> == 1000);
但它既不在 GCC 也不在 clang 上编译。
是否可以指定模板非类型模板参数? 或者,可以递归地编写它,但很高兴知道是否可以像上面那样编写它。
请注意,这个问题看起来类似于 Template template non-type parameter 但是非类型模板参数放在嵌套模板参数列表中,而不是放在主参数列表中。
【问题讨论】:
标签: c++ constexpr c++20 template-templates