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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:07:44+00:00 2026-06-14T14:07:44+00:00

Given the following class based on containers of shared pointers, class Foo; class Bar

  • 0

Given the following class based on containers of shared pointers,

class Foo;

class Bar {
public:
  // ...
  const std::vector<boost::shared_ptr<const Foo> >& getFoos() const { return foos_; }
private:
  std::vector<boost::shared_ptr<Foo> > foos_;
};

which will not compile because

invalid initialization of reference of type ‘const std::vector<boost::shared_ptr<const Foo>, std::allocator<boost::shared_ptr<const Foo> > >&’ from expression of type ‘const std::vector<boost::shared_ptr<Foo>, std::allocator<boost::shared_ptr<Foo> > >’

The foos_ member needs to point to mutable Foo objects for internal use by the Bar object, but I don’t want client code calling getFoos() to be able to modify anything.

Removing the const qualifier from Foo in the getFoos() return type fixes this. However, I understand that while std::vector propagates its constness to its elements, boost::shared_ptr does no such thing to the object it points to (naturally). Thus, it seems to me getFoos() no longer observes its const qualifier (even though the compiler doesn’t complain), because client code can modify the Foo objects pointed to by the shared pointers returned.

Am I correct? If so, is there some way to write getFoos() so that it will return a const vector of const references to const objects without copying?

  • 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-14T14:07:45+00:00Added an answer on June 14, 2026 at 2:07 pm

    I could be wrong but I really don’t think you can achieve this.

    shared_ptr<Foo> can become a shared_ptr<const Foo> only through a construction of a new instance to shared_ptr<const Foo>

    A reference to shared_ptr<Foo> cannot become a reference to shared_ptr<const Foo>, simply because they are two different types.

    Here you are trying to get a reference to vector<shared_ptr<Foo>> into the form of const vector<shared_ptr<const Foo>>.

    The first const is perfectly fine. Since it’s okay to assign a reference to the same reference type with a const qualifier.

    But the second const is not, as you are literally trying to convert a reference to a vector of Type A to a reference to a vector of Type B.

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

Sidebar

Related Questions

This is just an example, but given the following model: class Foo(models.model): bar =
Given a base class with the following interface: public class Base { public virtual
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class
Given the following: class BC { public void Display() { System.Console.WriteLine(BC::Display); } } class
Given the following inheritance tree: I have a public class BaseForm : Form This
Given the following template: template<class T> class Container { private: boost::function<T> f; }; ...
Given the following models: team.rb class Team < ActiveRecord::Base has_many :events, :dependent => :destroy
Given the following models: class Post(models.Model): title = models.CharField(max_length=200) html = models.TextField() class PostTag(models.Model):

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.