Generic visitor base class template in C++ - overload issue

﹥>﹥吖頭↗ 提交于 2020-01-13 03:55:40

问题


I thought it would be a simple exercise to write a generic visitor base class template. The goal is to be able to write

typedef visitor<some_base, some_derived1, some_derived2> my_visitor;

...and then have my_visitor be a type that is functionally equivalent to

struct my_visitor {
    virtual void visit(some_base&) {}
    virtual void visit(some_derived1&) {}
    virtual void visit(some_derived2&) {}
};

which i can inherit with actually useful derived visitor classes for that type hierarchy, which override the different visit() versions as needed. I want it to work for any number of types having any inheritance relations, and i don't want to use any hacks that reimplement virtual functions using type_info comparisons. This is what I came up with:

#include <cstdlib>
#include <iostream>
#include <vector>


/** This is the generic part that would go in a visitor.hpp header. */
template <typename T> struct visitor_base {
    virtual void visit(T&) {};
};

template <typename... T> struct visitor : visitor_base<T>... {};


/** This is the part that is specific to a certain type hierarchy. */
struct base;
struct derived1;
struct derived2;

typedef visitor<base, derived1, derived2> my_visitor;


/** This is the type hierarchy. */
struct base {
    virtual void accept(my_visitor& v) { v.visit(*this); }
};

struct derived1 : base {
    derived1() : i(42) {}
    virtual void accept(my_visitor& v) { v.visit(*this); }
    int i;
};

struct derived2 : base {
    derived2() : f(2.79) {}
    virtual void accept(my_visitor& v) { v.visit(*this); }
    float f;
};


/** These are the algorithms. */
struct print_visitor : my_visitor {
    void visit(base&) { std::cout<<"that was a base."<<std::endl; }
    void visit(derived1& d) { std::cout<<"that was "<<d.i<<std::endl; }
    void visit(derived2& d) { std::cout<<"that was "<<d.f<<std::endl; }
};

struct randomise_visitor : my_visitor {
    void visit(derived1& d) { d.i = std::rand(); }
    void visit(derived2& d) { d.f = std::rand() / float(RAND_MAX); }
};


int main() {
    std::vector<base*> objects { new base, new derived1, new derived2,
                                 new derived2, new base };

    print_visitor p;
    randomise_visitor r;

    for (auto& o : objects) o->accept(p);
    for (auto& o : objects) o->accept(r);
    for (auto& o : objects) o->accept(p);
}

The problem is that this does not compile. GCC says

silly_visitor.cpp: In member function ‘virtual void base::accept(my_visitor&)’:
silly_visitor.cpp:24:42: error: request for member ‘visit’ is ambiguous
silly_visitor.cpp:8:16: error: candidates are: void visitor_base<T>::visit(T&) [with T = derived2]
silly_visitor.cpp:8:16: error:                 void visitor_base<T>::visit(T&) [with T = derived1]
silly_visitor.cpp:8:16: error:                 void visitor_base<T>::visit(T&) [with T = base]

Basically, the problem is that since the different visit() member functions are declared in different classes, they are not seen as candidates for overload resolution but just as ambiguous member access. The normal trick for forcing the compiler to consider inherited functions for overload resolution is to redeclare them in the derived class with 'using' statements, but that is not feasible in this case since it would ruin the whole generic aspect of it.

So, apparently this was not as easy as I thought. Any ideas?


回答1:


The compiler doesn't know which base-class' visit function to call. See this question of mine. As such, as you correctly said, you need to make the functions available in the visitor class with using declarations. Sadly, you can't just use using visitor_base<T>::visit...;, as that is not a valid pattern. You have to recursively inherit from one base after another and everytime bring the base-class visits into the scope of the derived class:

template <typename T>
struct visitor_base {
    virtual void visit(T&) {};
};

template <typename Head, typename... Tail>
struct recursive_visitor_base
  : visitor_base<Head>
  , recursive_visitor_base<Tail...>
{
  using visitor_base<Head>::visit;
  using recursive_visitor_base<Tail...>::visit;
};

template<class T>
struct recursive_visitor_base<T>
  : visitor_base<T>
{
  using visitor_base<T>::visit;
};

template <typename... T>
struct visitor
  : recursive_visitor_base<T...>
{
  using recursive_visitor_base<T...>::visit;
};

Live example on Ideone (I had to tweak the partial spec a bit, since GCC 4.5.1 is a bit buggy in that part. Clang compiles the code shown in this answer just fine). Output:

that was a base.
that was 42
that was 2.79
that was 2.79
that was a base.
=================
that was a base.
that was 1804289383
that was 8.46931e+08
that was 1.68169e+09
that was a base.


来源:https://stackoverflow.com/questions/9024177/generic-visitor-base-class-template-in-c-overload-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!