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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:13:21+00:00 2026-05-21T02:13:21+00:00

I can’t for the life of me figure out what’s wrong with this code:

  • 0

I can’t for the life of me figure out what’s wrong with this code:

ClassA & doSomething (std::set<boost::shared_ptr<ClassB const> const > const & someSet)
{
  std::set<boost::shared_ptr<ClassB> > secondSet;
  for (std::set<boost::shared_ptr<ClassB const> const >::const_iterator it = someSet.begin(); it != someSet.end(); it++)
  {
    if (checkSomething(*it))
      secondSet.insert(boost::const_pointer_cast<ClassB>(*it));
  }
}

When I try to compile, I get the following errors at line 4 (the start of the for loop) from g++:

/usr/include/c++/4.4/ext/new_allocator.h:79: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = const boost::shared_ptr<const ClassB>]’ cannot be overloaded
/usr/include/c++/4.4/ext/new_allocator.h:76: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const boost::shared_ptr<const ClassB>]’

If I change the std::set declaration to contain non-const boost::shared_ptr the code compiles perfectly, but that mean I won’t be able to enforce const-correctness in my code.

Has anyone got any idea as to what might be causing these errors? I’ve searched both Google & StackOverflow without any luck.

Here is a minimum (non-)working example:

#include <set>
#include <boost/shared_ptr.hpp>

class ClassB;

class ClassA
{
   public:
      ClassA & doSomething (std::set<boost::shared_ptr<ClassB const> const > const & someSet);
};

ClassA & doSomething (std::set<boost::shared_ptr<ClassB const> const > const & someSet)
{
   std::set<boost::shared_ptr<ClassB> > secondSet;
   for (std::set<boost::shared_ptr<ClassB const> const >::const_iterator it = someSet.begin(); it != someSet.end(); it++)
   {
      if (checkSomething(*it))
         secondSet.insert(boost::const_pointer_cast<ClassB>(*it));
   }

   return (*this);
}
  • 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-21T02:13:22+00:00Added an answer on May 21, 2026 at 2:13 am

    but that mean I won’t be able to enforce const-correctness in my code

    I think you may be wrong there. As long it is a smart pointer to a const object, you may only manipulate the pointers in the set, not the objects referred to. This is kind of the whole point, because a set cannot organize itself if it cannot touch the values.

    It’s the classical difference between

    const char* x; // pointer to const char
    const char* const x; // constant pointer to const char
    char* const x; // constant pointer, to non-const char
    

    Only this time with smartpointers. You can also verify for yourself that this simple test will fail for const items:

    #include <set>
    
    int main()
    {
        std::set<const int> a;
        a.insert(1);
        return 1;
    }
    

    Once you remove ‘const’ it will work.


    OT: If you want some container that has some kind of ‘key’ protection, you should perhaps look at map since in the value_type (std::pair) the key (.first) is always const – IIRC. I still think the point is moot with regard to const correctness of the container’s elements.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can anyone explain to me why this program: for(float i = -1; i <
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.