Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:21:35+00:00 2026-05-16T21:21:35+00:00

Is the Visitor Pattern the fastest way to accomplish method parameter type identification (effectively

  • 0

Is the Visitor Pattern the fastest way to accomplish method parameter type identification (effectively single dispatch on a parameter, not a member’s class) in C++? I might know the exact method(s) I want to invoke on elements of not-yet-know subtype, so invariably making an additional virtual method call like V::visit(A *) in A::accept(V &v) { v.visit(this); } is undesirable.

// Is the Visitor pattern recommended here?  (E inherits D inherits B.)
class Foo {
public:
  virtual void visit(B *) { result = 3; }
  virtual void visit(D *) { result = 4; }
  virtual void visit(E *) { result = 5; }
private:
  int result;
}; // class Foo

// Need to add generic interface to B and its children ...
class B {
public:
  virtual void accept(class Foo &f) { f.visit(this); }
}; // class B

I’d like something functionally equivalent the following but with O(1) cost, which is AFAIK not possible with dynamic_cast<> or typeid() ladders, since std::type_info can’t be a constexpr/switchable.

// O(n) search cost might get nasty with bigger hierarchies.
int foo(B *b) {
  if (typeid(b) == typeid(B *)) { return 1; }
  if (typeid(b) == typeid(D *)) { return 2; }
  if (typeid(b) == typeid(E *)) { return 3; }
  return -1;
}

What are my options here? Thanks for the advice!

Edit: Changed sample code to feed results through field, such that multiple signatures aren’t needed for different method types. Thanks, Maurice!

Final decision: In addition to not liking the mandatory double dispatch cost of the Visitor Pattern, I also wanted to avoid the interface bloat of overloading foo(), but I don’t think that there is a known clean pattern to do this. I ended up just doing straight static overloads and called it a day. Anyway, my wanting to encapsulate overloading inside a function is probably a questionable goal at best. Thanks, Maurice for the response.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:21:35+00:00Added an answer on May 16, 2026 at 9:21 pm

    In fact, the interfaces need not be duplicated. The subclasses of the visitor can handle the details of the operation. In your case:

    class Visitor {
        virtual void visit(B*) = 0;
        virtual void visit(D*) = 0;
        virtual void visit(E*) = 0;
    }
    
    class Foo: public Visitor {
    private:
        int result;
    public:
        void visit(B*) { result = 3; }
        void visit(D*) { result = 4; }
        void visit(E*) { result = 5; }
        int apply(A* a) {
            a->accept(this);
            return result;
        }
    }
    

    So, only one accept() method is needed in each class.

    All the alternatives to the visitor pattern I can think of involve some kind of run-time searching, so yes, IMHO, the visitor pattern is the fastest way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between Double Dispatch and the Visitor Pattern?
In a typical implementation of the Visitor pattern, the class must account for all
Is there any difference between double dispatch and visitor pattern? I'm working with Java
experimenting with Visitor pattern and generic method I found a kind of discrepancy in
I'm trying to use the Visitor Pattern and I have as follows: public class
I have following class diagram (visitor pattern implementation): Expected result: 1) WiredVisitor should visit
Church encoding (aka Visitor Pattern) is a way of representing data as functions: instead
The Visitor pattern allows operations on objects to be written without extending the object
My collegue suggested me to write a visitor pattern to navigate the AST. Can
I'm trying to use the visitor pattern to serialize the contents of objects. However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.