3

I'm reading the book by Alexandrescu, and I've run into the Acyclic Visitor pattern. I think that it's possible to get rid of the macross that calls AcceptImpl method of the BaseVisitable class. Could you tell me, whether the following implementation bellow conforms the standard?

class BaseVisitor
{
public:
    virtual ~BaseVisitor() {}
};

template <class SpecificVisitable>
class SpecificVisitor
{
public:
    virtual void Visit(SpecificVisitable& t) = 0;

protected:
    ~SpecificVisitor() {}
};

template <class SpecificVisitable>
class BaseVisitable
{
public:
    void Accept(BaseVisitor& visitor)
    {
        SpecificVisitor<SpecificVisitable>& specificVisitor = dynamic_cast<SpecificVisitor<SpecificVisitable>&>(visitor);
        specificVisitor.Visit(static_cast<SpecificVisitable&>(*this));
    }
protected:
    ~BaseVisitable() {}
};

class A : public BaseVisitable<A>
{
public:
    void PrintA() { std::cout << "A\n"; }
};

class B : public BaseVisitable<B>
{
public:
    void PrintB() { std::cout << "B\n"; }
};

class PrintVisitor final:
    public BaseVisitor,
    public SpecificVisitor<A>,
    public SpecificVisitor<B>
{
public:
    virtual void Visit(A& a) override
    {
        a.PrintA();
    }

    virtual void Visit(B& b) override
    {
        b.PrintB();
    }
};

int main()
{
    A a;
    B b;

    PrintVisitor visitor;
    a.Accept(visitor);
    b.Accept(visitor);
}
Alex
  • 392
  • 1
  • 12
  • the dynamic cast to a reference will result in an exception if the visitor does not support the specific type. Is that the desired behaviour? – Richard Hodges Mar 03 '17 at 10:28
  • 1
    Compiles without any errors/warnings with `-std=c++14 -Wall -pedantic` so it conforms the standard. http://coliru.stacked-crooked.com/a/3d92d105649750a7 – mpiatek Mar 03 '17 at 10:30
  • Thanks. I ran it with those options and got no errors or warnings. It looks like it's possible to write this pattern without macros. I wonder why Alexandrescu did not do that. – Alex Mar 03 '17 at 10:38

1 Answers1

0

I agree with the commenters to the original post that state that a dynamic_cast to a reference will fail. You are not testing that situation, so your code will of course work just fine. But, in the general case, it's wrong.

I recommend that you instead rewrite BaseVisitable to instead cast to a pointer and check that pointer before dispatching on it. Something like

if (auto sv = dynamic_cast<SpecificVisitor<SpecificVisitable>*>(visitor))
  sv->visit(*this)
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166