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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:51:06+00:00 2026-05-22T11:51:06+00:00

According to this page you can implicitly convert shared_ptr<Foo> to shared_ptr<const Foo> . That

  • 0

According to this page you can implicitly convert shared_ptr<Foo> to shared_ptr<const Foo>. That makes good sense.

However, I run into an error when I try to convert a std::vector containing shared_ptr<Foo> to one containing shared_ptr<const Foo>.

Is there a good way to achieve this conversion?

  • 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-22T11:51:07+00:00Added an answer on May 22, 2026 at 11:51 am

    No: std::vector<shared_ptr<Foo> > and std::vector<shared_ptr<const Foo> > are different types, so you can’t treat an object of one as an object of the other type.

    If you really need a std::vector<shared_ptr<const Foo> >, you can easily create one with shared_ptrs to the same elements as the original:

    std::vector<shared_ptr<Foo> > v;
    std::vector<shared_ptr<const Foo> > cv(v.begin(), v.end());
    

    However, if you write your code in terms of iterators, you shouldn’t have any problem with this. That is, instead of using

    void f(const std::vector<shared_ptr<const Foo> >&);
    
    // used as:
    std::vector<shared_ptr<const Foo> > v;
    f(v);
    

    you should be using

    template <typename ForwardIterator>
    void f(ForwardIterator first, ForwardIterator last);
    
    // used as:
    std::vector<shared_ptr<const Foo> > v;
    f(v.begin(), v.end());
    

    This way, the function f just requires that it gets a range of things that are usable as pointers to const Foo (or shared_ptr<const Foo>s, if the function assumes that the range contains shared_ptrs).

    When a function takes a range instead of a container, you decouple the function from the underlying data: it no longer matters what the data actually is or how it is stored, so long as you can use it in the way that you need to use it.

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

Sidebar

Related Questions

ANY DOM element can be made resizable according to this page: http://jqueryui.com/demos/resizable/ However, it
according to this page one can't use code like if variable = something(): #do
According to this page, it's possible to use TClientDataset as an in-memory dataset, completely
According this MSDN article HttpApplication .EndRequest can be used to close or dispose of
According to this discussion , the iphone agreement says that it doesn't allow loading
According to this site I can simply write $user = getlogin(); but the group
According to this website , you can change to command key sequence used by
How can I check check-boxes (on page load) according to value of check-boxes using
According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when
According to this http://perldoc.perl.org/UNIVERSAL.html I shouldn't use UNIVERSAL::isa() and should instead use $obj->isa() or

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.