可变参数模板类:是否可以为每个可变参数模板参数实现一个唯一的成员函数?

时间:2016-08-07 18:50:50

标签: c++ variadic-templates template-classes

我使用Visitor模式实现反射而不依赖于RTTI。 我的问题是:

我想实现一个Visitor,它可以将从同一个BaseItem类派生的不同类DerivedItem1,DerivedItem2等转换为此BaseItem类。

基类和派生类之一如下所示:

class BaseItem : public AbstractItem
{
    virtual ~BaseItem(){}
    virtual void visit(AbstractVisitor &v)
    {
        v.handle(*this);
    }
}

class DerivedItem1 : public BaseItem
{
    virtual ~DerivedItem(){}
    virtual void visit(AbstractVisitor &v)
    {
        v.handle(*this);
    }
}

访客类:

class BaseVisitor : public AbstractVisitor
{
    virtual ~BaseVisitor(){}
    void handle(BaseItem &item)
    {
        // <-- stuff to do for all classes derived from BaseItem
    }
}

无法像这样实现BaseVisitor, 因为DerivedItem::visit(BaseVisitor)没有将自己投射到它的Base类 并且BaseVisitor::handle(BaseItem &v)永远不会被召唤。

我想将访问者实现为模板类,将基类和所有派生类作为模板参数,如下所示:

template <typename BaseT, typename... DerivedT>
class BaseVisitor : public AbstractVisitor
{
public:
    virtual ~BaseVisitor(){}

    // unpacking all DerivedT should happen here
    // DerivedT_X are the packed template arguments ...DerivedT
    void handle(DerivedT_1 &item)
    {
        // <-- cast item to BaseT, do stuff, return BaseT* to caller
    }

    void handle(DerivedT_2 &item)
    {
        // <-- cast item to BaseT, do stuff, return BaseT* to caller
    }
};

C ++能否以某种方式让编译器自己生成这个成员函数?

2 个答案:

答案 0 :(得分:0)

使用CRTP和可变参数模板,您可以执行以下操作:

// The generic visitor interface
template <typename ... Ts>
class IVisitor;

template <> class IVisitor<>
{
public:
    virtual ~IVisitor() = default;
};

template <typename T> class IVisitor<T>
{
public:
    virtual ~IVisitor() = default;
    virtual void visit(const T&) = 0;
};

template <typename T, typename...Ts>
class IVisitor<T, Ts...> : IVisitor<T>, IVisitor<Ts...>
{
public:
    using IVisitor<T>::visit;
    using IVisitor<Ts...>::visit;
    virtual ~IVisitor() = default; 
};

// Helper for the concrete visitor using CRTP
template <typename Derived, typename Base, typename...Ts>
struct CRTPVisitorImpl;

template <typename Derived, typename Base>
struct CRTPVisitorImpl<Derived, Base> : Base {};

template <typename Derived, typename Base, typename T>
struct CRTPVisitorImpl<Derived, Base, T> : virtual Base
{
    using Base::visit;
    void visit(const T& t) override { static_cast<Derived&>(*this).doVisit(t); }    
};

template <typename Derived, typename Base, typename T, typename ... Ts>
struct CRTPVisitorImpl<Derived, Base, T, Ts...> :
    CRTPVisitorImpl<Derived, Base, T>,
    CRTPVisitorImpl<Derived, Base, Ts...>
{
    using CRTPVisitorImpl<Derived, Base, T>::visit;
    using CRTPVisitorImpl<Derived, Base, Ts...>::visit;
};

// The generic Visitor
template <typename Derived, typename Base>
struct CRTPVisitor;

template <typename Derived, typename ... Ts>
struct CRTPVisitor<Derived, IVisitor<Ts...>> :
    CRTPVisitorImpl<Derived, IVisitor<Ts...>, Ts...>
{};

// Helper to write visited
template <typename Derived, typename Base, typename Visitor>
struct Visited : Base
{
    void accept(Visitor& visitor) const override {
        visitor.visit(static_cast<const Derived&>(*this));
    }
};

用法:

struct ShapeVisitorPrinter : CRTPVisitor<ShapeVisitorPrinter, IShapeVisitor>
{
    template <typename T>
    void doVisit(T&& t) const {
        t.print();
    }
};

每个Ivisitor::visit使用CRTP调用doVisit,因此您只需通过模板/重载/基类来覆盖每个案例。

Demo

答案 1 :(得分:0)

您不能像在问题中描述的那样在模板定义的主体上解压缩参数包,但是您可以使用CRTP来组装一个类,该类继承了每个类型的模板化特化的层次结构 - 您提供的参数:

#include <iostream>

template<class L, class... R> struct X;

template<class L>
struct X<L> { void handle(L& i) { std::cout << i.f() << "\n"; } };

template<class L, class... R>
struct X : public X<L>, public X<R...> { using X<L>::handle; using X<R...>::handle; };

struct A1 {
    int f() { return 1; }
};

struct A2 {
    int f() { return 2; }
};

struct B {
    int f() { return 10; }
};

struct B1 : public B {
    int f() { return 11; }
};

struct B2 : public B1 {
    int f() { return 12; }
};

int main() {
    X<A1, A2> x1;
    A1 a1; A2 a2;
    x1.handle(a1);
    x1.handle(a2);

    X<B, B1, B2> x2;
    B b; B1 b1; B2 b2;
    x2.handle(b);
    x2.handle(b1);
    x2.handle(b2);
}