这是我的代码:
template<
template <typename TSEvent,
typename ...TSEvents> typename V,
typename... Filtered>
constexpr auto filter() {
if constexpr(sizeof...(TSEvents) == 0) {
return type_list<Filtered...>{};
}
if constexpr(is_default_constructible<TSEvent>::value) {
return filter<<TSEvents...>, Filtered...>();
}
return filter<<TSEvents...>, Filtered...>();
}
但是我收到此错误,size...(TSEvents)
,TSEvents
未声明。我是否可以在嵌套模板中访问 TSEvents
?
最佳答案
通常通过另一个间接级别,并且通常是我们可以专门化的struct
。
例如:
namespace detail
{
template<class...>
struct filter_t;
template<template<class, class...> class V, class TSEvent, class... TSEvents, class... Filtered>
struct filter_t<V<TSEvent,TSEvents...>, Filtered...>
{
static constexpr auto filter() {
return sizeof...(TSEvents);
}
};
} // detail
template<class... T>
constexpr auto filter()
{
return detail::filter_t<T...>::filter();
}
template<class T, class...U>
struct type_list{};
int main()
{
std::cout << filter<type_list<int, int, int>, int>();
}
关于templates - 我怎样才能访问这个嵌套模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51050584/