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

  • Home
  • SEARCH
  • 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 8906489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:40:10+00:00 2026-06-15T02:40:10+00:00

I am confusing the following problem by identifying the a specific type within boost::variant

  • 0

I am confusing the following problem by identifying the a specific type within boost::variant and pass it as member function argument within a class object. Consider the following code

typedef boost::variant<int, string, double> Variant;

class test{ 

  void func1 (Variant V); 
  void func2 (string s); 
  void func3 ();


}; 


test::func1(Variant V){
    // How can I identify the type of V inside the body of this function? 
Or how can I call the apply_visitor inside this function.
  /*
  if(v.type() == string) 
    func2(); 
  else if(v.type() == double) 
    funct3(); 
  */
}
int test::func2(){ cout << "func3" << endl;}
int test::func3(){ cout << "func4" << endl;}
...

int main ()
{
  test t;
      Variant V = 3;
      t.func1(V); 
      V = "hello"; 
      t.func1(V);

}

I thought sure about implementing a visitation class/structure (apply_visitor ) within class test. However I got stuck by calling a outer member function namely func3(string s) from the overloaded operator implemented in the visitor class.

  • 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-15T02:40:11+00:00Added an answer on June 15, 2026 at 2:40 am

    The visitor pattern reification will make the compiler do the type-checking for you. All you need to do is tell the compiler what to do when a string is in the Variant:

    (check out the example at
    http://www.boost.org/doc/libs/1_35_0/doc/html/variant/tutorial.html)

    struct my_dispatcher : public boost::static_visitor<> {
    
        test* t;
        my_dispatcher(test* t): t(t) {}
    
        void operator()( string s ) { t.func3(s); }
        void operator()( double d ) { t.func4(d); }
        //... for each supported type
    };
    

    and use boost::apply_visitor to choose the correct function:

    int main ()
    {
      test t;
      my_dispatcher dispatcher(&t);
    
      Variant V = 3;
      boost::apply_visitor( dispatcher, v );
    
      V = "hello"; 
      boost::apply_visitor( dispatcher, v );    
    }
    

    The my_dispatcher(&t) will create an object of your static_visitor implementation, that will be used by the apply_visitor magic.

    Hope that’s what your were looking fore, since your question wasn’t really clear.

    Note: alternatively, you derive your test from static_visitor.

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

Sidebar

Related Questions

I have within an MDX query the following calculated member: MEMBER [Asset].[Class].[Fixed Income Derivatives
I encountered the following confusing problem: A DB table was updated through CSV file
I'm facing a confusing problem. If you create a table like the following, you'll
The following code seemed really confusing to me since it provided two different outputs.The
The title is a little confusing so I will explain. I have the following
I have a very confusing problem, and I am able to solve it, but
Sorry about the confusing title, I had a hard time describing the problem. I
I have what is, to me, a confusing situation. The following code is producing
I have the following (greatly simplified) classes: public class Customer { [Required(ErrorMessageResourceName=Required, ErrorMessageResourceType=typeof(ResourcesCommon.ValidationStrings))] public
Problem I know there is a lot of ways of doing Model validation within

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.