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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:45:35+00:00 2026-05-20T20:45:35+00:00

I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()

  • 0

I have class containing an std::set of boost::weak_ptr<T>. I have two functions begin() and end() that return an iterator to the container. However, I don’t want clients to be able to modify T. Simply returning a const_iterator won’t work, because the T pointed to by the boost::weak_ptr will be editable.

What I want to do is return a const_iterator to std::set<boost::weak_ptr<T const> >. Casting from std::set<boost::weak_ptr<T> >::const_iterator does not work. Is there any way to get the behaviour I want?

  • 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-20T20:45:36+00:00Added an answer on May 20, 2026 at 8:45 pm

    You can write a transform iterator to convert the weak_ptr<T> to a weak_ptr<const T>. Since you’re already using Boost, you can use boost::transform_iterator:

    #include <boost/iterator/transform_iterator.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/weak_ptr.hpp>
    
    #include <set>
    
    // Functor to transform a weak_ptr<T> to a weak_ptr<const T>
    template <typename T>
    struct make_weak_ptr_const
        : std::unary_function<boost::weak_ptr<T>, boost::weak_ptr<const T> >
    {
        boost::weak_ptr<const T> operator()(const boost::weak_ptr<T>& p) const
        {
            return p;
        }
    };
    
    struct S { };
    
    // Container demonstrating use of make_weak_ptr_const:
    struct my_awesome_container
    {
        typedef std::set<boost::weak_ptr<S> > BaseSet;
        typedef BaseSet::const_iterator       BaseIterator;
    
        typedef boost::transform_iterator<
                    make_weak_ptr_const<S>, 
                    BaseIterator
                > iterator;
    
        iterator begin() const 
        {
            return TransformedIterator(data.begin());
        }
    
        iterator end() const
        {
            return TransformedIterator(data.end());
        }
    
        std::set<boost::weak_ptr<S> > data;
    };
    

    If you don’t want to use boost::transform_iterator, it is a straightforward task to write your own. I showed how to do this in an answer to another question.

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

Sidebar

Related Questions

I have a class that requires a boost::variant containing shared pointers to various types
I have a class containing Linq To SQL objects that are used to populate
I have an example class containing two data points: public enum Sort { First,
I have a class A containing two pointers to objects of another class B.
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a class containing a std::unique_ptr<> and I want to put instances of
When I have class containing a static constructor, is that constructor called when the
I have a class containing methods to fill DropDowns, return DataSet, return Scalar or
Right now I have a vector std::vector<char> myVector(4) containing any combination of a set
I have a class with a container (containing pointer) as a member: MyClass{ private:

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.