模板非类型模板参数
template non-type template parameter
我正在尝试使用 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 上编译。
是否可以指定模板非类型模板参数?
或者,可以递归地编写它,但很高兴知道是否可以像上面那样编写它。
请注意,这个问题看起来类似于
但是非类型模板参数被放置在嵌套模板参数列表中,而不是主参数列表中。
您不能直接将参数引用到模板模板参数。它们不在范围内。但是你可以这样做:
#include <utility>
template <class>
class pow10_helper {};
template <template <typename T, T ...> class IntSeq, class T, T ... Ints>
struct pow10_helper<IntSeq<T, Ints...>> {
static constexpr long value = ((Ints, 10) * ...);
};
template <size_t pow>
static constexpr long pow10 = pow10_helper<std::make_index_sequence<pow>>::value;
#include <iostream>
int main() {
size_t pow = pow10<3>;
std::cout << pow << std::endl; // prints 1000
}
也就是说,有更简单的实现方法 pow10
:
template <size_t pow>
struct pow10 { static constexpr size_t value = 10 * pow10<pow-1>::value; };
template <> struct pow10<0> { static constexpr size_t value = 1; };
int main() {
size_t pow = pow10<3>::value;
std::cout << pow << std::endl; //prints 1000
}
你可以这样做:
#include <utility>
template<class T>
static constexpr long pow10_helper;
template<class T, T... Is>
static constexpr long pow10_helper<std::integer_sequence<T, Is...>> = ((Is, 10) * ...);
template <std::size_t exp>
static constexpr long pow10 = pow10_helper<std::make_index_sequence<exp> >;
static_assert(pow10<3> == 1000);
我正在尝试使用 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 上编译。
是否可以指定模板非类型模板参数? 或者,可以递归地编写它,但很高兴知道是否可以像上面那样编写它。
请注意,这个问题看起来类似于
您不能直接将参数引用到模板模板参数。它们不在范围内。但是你可以这样做:
#include <utility>
template <class>
class pow10_helper {};
template <template <typename T, T ...> class IntSeq, class T, T ... Ints>
struct pow10_helper<IntSeq<T, Ints...>> {
static constexpr long value = ((Ints, 10) * ...);
};
template <size_t pow>
static constexpr long pow10 = pow10_helper<std::make_index_sequence<pow>>::value;
#include <iostream>
int main() {
size_t pow = pow10<3>;
std::cout << pow << std::endl; // prints 1000
}
也就是说,有更简单的实现方法 pow10
:
template <size_t pow>
struct pow10 { static constexpr size_t value = 10 * pow10<pow-1>::value; };
template <> struct pow10<0> { static constexpr size_t value = 1; };
int main() {
size_t pow = pow10<3>::value;
std::cout << pow << std::endl; //prints 1000
}
你可以这样做:
#include <utility>
template<class T>
static constexpr long pow10_helper;
template<class T, T... Is>
static constexpr long pow10_helper<std::integer_sequence<T, Is...>> = ((Is, 10) * ...);
template <std::size_t exp>
static constexpr long pow10 = pow10_helper<std::make_index_sequence<exp> >;
static_assert(pow10<3> == 1000);