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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:45:02+00:00 2026-05-15T14:45:02+00:00

struct A { std::string get_string(); }; struct B { int value; }; typedef boost::variant<A,B>

  • 0
  struct A
  {
    std::string get_string();
  }; 

  struct B
  {
    int value;
  }; 

  typedef boost::variant<A,B> var_types;
  std::vector<var_types> v;

  A a;
  B b;

  v.push_back(a);
  v.push_back(b);

How can I iterate iterate through the elements of v to get access to the a and b objects ?

I’m able to do it with boost::get but the syntax is really cumbersome.:

std::vector<var_types>:: it = v.begin();
while(it != v.end())
{
    if(A* temp_object = boost::get<A>(&(*it)))    
      std::cout << temp_object->get_string();
    ++it;
}

I’ve tried to use the visitation technique but I didn’t get too far and the code is not working :

template<typename Type>
class get_object
  : public boost::static_visitor<Type>
{
public:

  Type operator()(Type & i) const
  {
    return i;
  }

};

...

while(it != v.end())
{
    A temp_object = boost::apply_visitor(get_object<A>(),*it);
    ++it;
}

EDIT 1

A hackish solution is :

class get_object
  : public boost::static_visitor<A>
{
public:

  A operator()(const A & i) const
  {
    return i;
  }

  A operator()(const B& i) const
  {
    return A();
  }        
};
  • 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-15T14:45:03+00:00Added an answer on May 15, 2026 at 2:45 pm

    Edit:
    If it’s as UncleBens suggests, then you can simply do this:

    BOOST_FOREACH(var_types& vt, v) {
        if (vt.which() == 0)  
            cout << get<A>(vt).get_string() << endl;
    }
    

    Original:
    The template parameter for static_vistor is the return type of its methods. This means the two classes need to share a common return type for a single visitor. It should look something like this:

    class ABVisitor : public static_visitor<string> {
    public:
        string operator()(const A& a) const {
            return a.get_string();
        }
        string operator()(const B& b) const {
            return lexical_cast<string>(b.value);
        }
    };
    

    Here’s an example of iteration using this visitor.

    BOOST_FOREACH(var_types& vt, v)
        cout << apply_visitor(ABVisitor(), vt) << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Templates that are installed through a VSIX don't get installed… May 16, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer That should be fine. You can even have a non-static… May 16, 2026 at 3:42 pm
  • Editorial Team
    Editorial Team added an answer Make them properties of a @ViewScoped bean. @ManagedBean @ViewScoped public… May 16, 2026 at 3:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
I define this structure: struct s_molecule { std::string res_name; std::vector<t_particle> my_particles; std::vector<t_bond> my_bonds; std::vector<t_angle>
Using boost::program_options, I can not get my own option type to compile when it
I'm currently using Boost's multi-index to help keep track of how many times a
Several comments on a recent answer of mine, What other useful casts can be
I tried to compile the example posted ( C++ Service Providers ) and failed
I'm trying to map some structs to some other instances, like this: template <typename
I'm trying to do something like this, enum Data_Property { NAME, DATETIME, TYPE, };
* ---Edit - now the whole sourse* When I debug it on the end,
I have a VS2008 C++ program where I'm wrapping a C API for use

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.