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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:40:01+00:00 2026-05-18T12:40:01+00:00

I have a boost::variant , which contains various types and I have a string

  • 0

I have a boost::variant, which contains various types and I have a string which needs to look like this: type=D,S. The values in the variant are D and S respectively, the key is ‘type’. It is a map<std::string, std::vector<variant> > where I’m now iterating the vector<variant> part

Now I first apply a static_visitor to my variant to do the appropriate conversion, which in this case might not be needed but for other type it would need conversion to a string.

Then I call this function called ConcatValues, part of a helper class. This class has a vector<string> v_accumulator defined, to hold temp results, as this function might be called several times in the while loop and I want to end up with a comma seperated value’s list.

The problem is however that my vector v_accumulator is always empty on each function call? How does that make any sense, seeing as it is a class variable.

while(variant_values_iterator != values.end())
{
          variant var = *variant_values_iterator;
        boost::apply_visitor( add_node_value_visitor( boost::bind(&SerializerHelper::ConcatValues, helper, _1, _2), key, result), var);
        variant_values_iterator++;
}



std::string SerializerHelper::ConcatValues(std::string str, std::string key)
{
    v_accumulator.push_back(str); //the previous value is not in this vector???
    std::stringstream ss;
    std::vector<std::string>::iterator it = v_accumulator.begin();

    ss << key;
    ss << "=";

    for(;it != v_accumulator.end(); it++)
    {
        ss << *it;
        if (*it == v_accumulator.back())
            break;
        ss << ",";
    }

    return ss.str();

}


class SerializerHelper
{
public:
    std::string ConcatValues(std::string str, std::string key);

private:
    std::vector<std::string> v_accumulator;
};

maybe there is an easier way to concatenate the values of D,S in the value part of my original key/value pair?

  • 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-18T12:40:02+00:00Added an answer on May 18, 2026 at 12:40 pm

    The problem might be that, although v_accumulator is a class member, boost::bind copies its arguments by default. This means that ConcatValues is called on a copy of the helper, with its very own v_accumulator vector.

    If you want a reference, you must use boost::ref :

    boost::apply_visitor(add_node_value_visitor(
        boost::bind(&SerializerHelper::ConcatValues, boost::ref(helper), _1, _2), key, result), var);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
OK, so I have this tiny little corner of my code where I'd like
I have the following definition of a boost::function object: typedef boost::function<std::string (std::string, std::string)> concat;
Does someone know if the boost::get for the boost::variant is a performance-consuming operation or
I would like to have a struct (or something similar) in C++, that will
I have the following struct in C++, and I wondered if I need to
I think HipHop for PHP is an interesting tool. It essentially converts PHP code
I've got some code that I've been using successfully for some years to implement

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.