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

The Archive Base Latest Questions

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

Hy there, I’m trying to adapt an existing code to boost::variant. The idea is

  • 0

Hy there,

I’m trying to adapt an existing code to boost::variant. The idea is to use boost::variant for a heterogeneous vector. The problem is that the rest of the code use iterators to access the elements of the vector. Is there a way to use the boost::variant with iterators?

I’ve tried

 typedef boost::variant<Foo, Bar> Variant;
 std::vector<Variant> bag;
 std::vector<Variant>::iterator it;
 for(it= bag.begin(); it != bag.end(); ++it){

 cout<<(*it)<<endl;
 }

But it didn’t work.

EDIT: Thank you for your help! But in my design, I need to get one element from the list and pass it around other parts of the code (and that can be nasty, as I’m using GSL). The idea of using an iterator is that I can pass the iterator to a function, and the function will operate on the return data from that specific element. I can’t see how to do that using for_each. I need to do something similar to that:

for(it=list.begin(); it!=list.end();++it) {
  for(it_2=list.begin(); it_2!=list.end();++it_2) {

     if(it->property() != it_2->property()) {

        result = operate(it,it_2);

       }
    }

}

Thanks!

  • 1 1 Answer
  • 2 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-15T04:45:33+00:00Added an answer on May 15, 2026 at 4:45 am

    Well of course there is. Dereferencing the iterators will naturally yield a boost::variant<...> reference or const-reference.

    However it does mean that the rest of code should be variant-aware. And notably use the boost::static_visitor to execute operations on the variants.

    EDIT:

    Easy!

    struct Printer: boost::static_visitor<>
    {
      template <class T>
      void operator()(T const& t) const { std::cout << t << std::endl; }
    };
    
    std::for_each(bag.begin(), bag.end(), boost::apply_visitor(Printer());
    

    Note how writing a visitor automatically yields a predicate for STL algorithms, miam!

    Now, for the issue of the return value:

    class WithReturn: boost::static_visitor<>
    {
    public:
      WithReturn(int& result): mResult(result) {}
    
      void operator()(Foo const& f) const { mResult += f.suprise(); }
      void operator()(Bar const& b) const { mResult += b.another(); }
    
    private:
      int& mResult;
    };
    
    
    int result;
    std::for_each(bag.begin(), bag.end(), boost::apply_visitor(WithReturn(result)));
    

    EDIT 2:

    It’s easy, but indeed need a bit of coaching 🙂

    First, we remark there are 2 different operations: != and operate

     struct PropertyCompare: boost::static_visitor<bool>
     {
       template <class T, class U>
       bool operator()(T const& lhs, U const& rhs)
       {
         return lhs.property() == rhs.property();
       }
     };
    
     struct Operate: boost::static_visitor<result_type>
     {
       result_type operator()(Foo const& lhs, Foo const& rhs);
       result_type operator()(Foo const& lhs, Bar const& rhs);
       result_type operator()(Bar const& lhs, Bar const& rhs);
       result_type operator()(Bar const& lhs, Foo const& rhs);
     };
    
    for(it=list.begin(); it!=list.end();++it) {
      for(it_2=list.begin(); it_2!=list.end();++it_2) {
    
        if( !boost::apply_visitor(PropertyCompare(), *it, *it_2) ) {
    
          result = boost::apply_visitor(Operate(), *it, *it_2));
    
        }
    
      }
    }
    

    For each is not that good here, because of this if. It would work if you could somehow factor the if in operate though.

    Also note that I pass not iterators but references.

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

Sidebar

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.