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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:24:29+00:00 2026-06-02T08:24:29+00:00

I am trying to do this: maitain two vectors of strings whereby one vector

  • 0

I am trying to do this: maitain two vectors of strings whereby one vector stores values and the second stores references of the same values. I thought using boost::reference_wrapper would do the trick but it seems it won’t. My platform is Visual C++ 2008.

std::vector<std::string> str_vec;
str_vec.push_back("abc");
str_vec.push_back("cde");
str_vec.push_back("fgh");

std::vector<boost::reference_wrapper<std::string> > str_view;
for(std::vector<std::string>::iterator it = str_vec.begin(); it != str_vec.end(); ++it)
{
  str_view.push_back(*it);
}

This is the error:

error C2664: ‘std::vector<_Ty>::push_back’ : cannot convert parameter 1 from std::basic_string<_Elem,_Traits,_Ax>’ to ‘const boost::reference

I could use boost::shared_ptr but I thought a reference better expresses my intent. This code can probably work in C++11 using std::reference_wrapper but that is not available to me right now.

  • 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-02T08:24:31+00:00Added an answer on June 2, 2026 at 8:24 am

    Yes, it can. The documentation states that it is CopyConstructible and Assignable which are required concepts for container template arguments. But you need to use boost::ref or boost::cref to create objects of type reference_wrapper. There is no implicit conversion and this is why your code does not work.

    Be aware that a slight difference between std::reference_wrapper and boost::reference_wrapper is that only the std version works with functors.

    Example:

    std::vector<std::string> str_vec;
    str_vec.push_back("abc");
    str_vec.push_back("cde");
    str_vec.push_back("fgh");
    
    std::vector<boost::reference_wrapper<std::string> > str_view;
    std::transform(begin(str_vec), end(str_vec),
                   std::back_inserter(str_view), boost::ref<std::string>);
    

    If you dislike that and would like to have implicit conversion from the original value, you might want to use:

    template<typename T>
    class my_ref_wrap : public boost::reference_wrapper<T> {
    public:
      my_ref_wrap(T& t) : boost::reference_wrapper<T>(t) {}
    };
    
    std::vector<my_ref_wrap<std::string> > str_view;
    std::copy(begin(str_vec), begin(str_vec),
              std::back_inserter(str_view));
    

    Although I would not do that.

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

Sidebar

Related Questions

I'm trying this for almost two hours now, without any luck. I have a
I'm trying this in two application; a console application and a web application. In
I'm trying to maintain a web site spread across two domains with the same
Im trying this, but when i press it it dont changes, it only displays
I'm trying this pattern: (r'^jobs/(?P<job_id>\d+)/$', job_handler) To work with jobs/ and jobs/{job_id}, but the
I am trying this code: entLoop:for(var i:*in entities) { for(var i2:*in ignoreEntities) { if(entities[i].type==ignoreEntities[i2])
I'm trying this: {:id => 5, :foos => [1,2,3]}.each {|k,v| v.to_s} But that's returning
i get this error when trying this: ERROR method name expected. How should i
Right now I'm trying this: int a = round(n); where n is a double
I've been trying this for hours, so maybe a fresh set of eyes will

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.