要获得好的错误消息,您必须将声明更改为
template<template<typename> class Cond, typename T, typename... Ts>
struct first_if_any;
所以
first_if_any<Cond>
错误:类模板“first\u if\u any”的模板参数太少
然后,您当前实现的问题是,您使用了您想要禁止的内容,我的意思是
(带有各种
first_if_any<Cond, T...>
哪里
T...
您可以使用更容易处理默认类型的中间类:
template<template<typename> class Cond, typename Default, typename... Ts>
struct first_if_any_or_default;
template<template<typename> class Cond, typename Default>
struct first_if_any_or_default<Cond, Default>
{
using type = Default;
static constexpr bool found = false;
};
template<template<typename> class Cond, typename Default, typename T, typename... Ts>
struct first_if_any_or_default<Cond, Default, T, Ts...>
{
private:
using next = first_if_any_or_default<Cond, Default, Ts...>;
public:
using type = typename std::conditional<Cond<T>::value,
T,
typename next::type>::type;
static constexpr bool found = Cond<T>::value || next::found;
};
template<template<typename> class Cond, typename First, typename... Ts>
struct first_if_any {
private:
using helper = first_if_any_or_default<Cond, First, First, Ts...>;
public:
using type = typename helper::type;
static constexpr bool found = helper::found;
};