C++ 折叠表达式中的减量模板参数
考虑以下代码:C++ 折叠表达式中的减量模板参数,c++,templates,C++,Templates,考虑以下代码: template<typename...> struct C { /* ... */ }; template<typename T, unsigned N> struct B { using type = /* ... */; }; template<size_t N, typename... Ts> struct A { using type = C<typename B<Ts, N-->::type..
template<typename...>
struct C
{ /* ... */ };
template<typename T, unsigned N>
struct B
{
using type = /* ... */;
};
template<size_t N, typename... Ts>
struct A
{
using type = C<typename B<Ts, N-->::type...>; // decrement N sizeof...(Ts) times
};
模板
结构C
{ /* ... */ };
模板
结构B
{
使用类型=/*…*/;
};
模板
结构A
{
使用type=C;//递减N个大小的…(Ts)次
};
比如说
typename A<5, int, long, void>::type
typename A::type
扩展到
C<typename B<int, 5>::type, typename B<long, 4>::type, typename B<void, 3>::type>
C
由于N
是一个const
值,因此此代码不会编译。还有别的办法吗?从N
namespace detail {
template<size_t N, typename... Ts, size_t... Is>
C<typename B<Ts, N - Is>::type...> A_impl(std::index_sequence<Is...>);
}
template<size_t N, typename... Ts>
struct A
{
using type = decltype(detail::A_impl<N, Ts...>(std::index_sequence_for<Ts...>{}));
};
名称空间详细信息{
模板
C A_impl(std::index_序列);
}
模板
结构A
{
使用type=decltype(detail::A_impl(std::index_sequence_for{}));
};
您可以使用未实际定义或调用的模板化静态constexpr
辅助函数:
template<size_t N, typename... Ts>
struct A
{
template<size_t... Is>
static constexpr auto type_getter(std::index_sequence<Is...>) -> C<typename B<Ts, N-Is>::type...>;
using type = decltype(type_getter(std::index_sequence_for<Ts...>{}));
};
模板
结构A
{
模板
静态constexpr自动类型获取程序(std::index\u序列)->C;
使用type=decltype(type_getter(std::index_sequence_for{}));
};
这是一个恒定的表达式。这就像尝试做
5--
;胡说八道,你想干什么?您需要解决的实际问题(如果有)是什么?为什么需要递减该值?顺便说一句,这不是折叠表达式。请在问题中包含错误消息,并显示真实代码(最好)。我发现将折叠中的..
和..
用于指示同一代码段中缺少的代码非常令人困惑N-1
有什么问题?看起来很有魅力。哇,这与我提出的非常相似,甚至是非类型模板的模板参数名size\u t…