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 8098415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:04:26+00:00 2026-06-05T22:04:26+00:00

If all the types in my boost::variant support the same method, is there a

  • 0

If all the types in my boost::variant support the same method, is there a way to call it generically (i.e. not calling it seperately for each method of the static_visitor)?

I’m trying to get something like this to work:

class A
{
    void boo() {}
};

class B
{
    void boo() {}
};

class C
{
    void boo() {}
};

typedef boost::variant<A, B, C> X;

void foo(X& d)
{
    x.boo();
}

but it fails to compile saying 'boo' : is not a member of 'boost::variant<T0_,T1,T2>'.

Currently, I have some classes all inherit from an interface so that their single shared method can be used polymorphically. I also want to be able to use the classes via a visitor as all other methods are unique to each concrete class. I was hoping boost::variant might be a better alternative to implementing my own visitor mechanism here. Is it?

  • 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-06-05T22:04:27+00:00Added an answer on June 5, 2026 at 10:04 pm

    There’s no direct way, but you can make the static_visitor pretty concise using templating.

    Modified from the boost docs:

    struct boo_generic : public boost::static_visitor<>
    {
        template <typename T>
        void operator()( T & operand ) const
        {
            operand.boo();
        }
    };
    

    Now you can do this:

    boost::apply_visitor( boo_generic(), v );
    

    Infact you can generalise this to take a function pointer of your base class:

    struct fn_generic : public boost::static_visitor<>
    {
       fn_generic( void (IBase::fn)() ) : fn_(fn) {}
       template<T> void operator() ( T & op ) const { op.*fn(); }
    }
    

    Then you can do:

    boost::apply_visitor( boo_generic( IBase::boo ), v );
    

    Or something like that – I’ve probably got my function pointer syntax wrong, but hopefully you get the idea.

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

Sidebar

Related Questions

Are all WCF endpoint types capable of the same things? IS the only difference
The following function is defined for all arithmetic types: template <class T> typename enable_if_c<boost::is_arithmetic<T>::value,
In boost::numeric::ublas , there are three sparse vector types . I can see that
I would like to call a function on all elements of a boost::fusion::vector. The
I've been using GROUP BY for all types of aggregate queries over the years.
I am creating a calculator application for all types of mathematical algorithms. However, I
I have to draw underlined-multiline text with all types of text alignment. I have
Currently we are developing a Java application to grab documents (all types can be
In my current framework's design, I peruse all types of specific assemblies and do
I want to print values of all the types like char, long... so on

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.