假设我有两节课:
template <typename X, typename Y> class Functor {};
template <typename Start, typename End, typename ... Functors> class Template {};
Template
有限制:
所有
Functors
必须为 Functor
类型
所有
Functor
必须处于链序列中,使得
Functor
必须将 Start
作为其 first 参数Functor
必须将 End
作为其 second 参数Functor
的 first 参数是其前面的 Functor
的 second参数
例如
Functor<A,B>, Functor<B, C>, Functor<C, D>, ...
等等
开始于:
char
结尾为:
long
Template<char, long, Functor<char, A>, Functor<A, B>, Functor<B, C>, Functor<C, long>> t;
1 2 3 4
|---------|---------|---------|---------|
argument: char A B C long
Functor #
= 1 Functor<char, A>,
2 Functor<A, B>,
3 Functor<B, C>,
4 Functor<C, long>
namespace ns
{
template <typename X, typename Y = X>
class Functor
{
public:
using first = X;
using second = Y;
Functor(X lVal) : x(lVal) {}
private:
X x;
};
template <typename Start, typename End, typename ... Functors>
requires(std::is_convertible_v<Functors, Functor> && ...) //error
class Template
{
// How does one use `std::is_convertible_v` on
// an un-specialized template class?
};
template <typename Start, typename End>
class Template<Start, End, Functor<Start, End>>
{};
}
问题:
std::is_convertible
(或任何其他元编程特征)?如果您已经完成了这一步,感谢您抽出宝贵的时间,并提前感谢您提供的任何信息。
以下似乎有效。这给出了
Functor
正确序列的基本检查,并将其转化为概念,或添加类型特征样式 using
包装器等......,可以作为家庭作业:
class Start;
class End;
template <typename X, typename Y> class Functor {};
template<typename required, typename NextFunctor,
typename ...RemainingFunctors> struct FunctorValidate;
template<typename required>
struct FunctorValidate<required, Functor<required, End>> {
typedef void type_t;
};
template<typename required, typename NextFunctorType,
typename FirstRemainingFunctor, typename ...RemainingFunctors>
struct FunctorValidate<required, Functor<required, NextFunctorType>,
FirstRemainingFunctor, RemainingFunctors...>
: FunctorValidate<NextFunctorType, FirstRemainingFunctor,
RemainingFunctors...>
{
};
template<typename ...AllFunctors> struct FunctorChain
: FunctorValidate<Start, AllFunctors...>
{
};
class A;
class B;
class C;
typedef FunctorChain<Functor<Start, A>,
Functor<A, B>,
Functor<B, C>,
Functor<C, End>>::type_t ok1;
typedef FunctorChain<Functor<Start, End>>::type_t ok2;
#if 0
typedef FunctorChain<Functor<A, B>,
Functor<B, C>,
Functor<C, End>>::type_t error1;
typedef FunctorChain<Functor<Start, A>,
Functor<A, B>,
Functor<B, C>>::type_t error2;
typedef FunctorChain<Functor<Start, A>,
Functor<B, C>,
Functor<C, End>>::type_t error3;
#endif