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

  • Home
  • SEARCH
  • 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 4556640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:23:43+00:00 2026-05-21T17:23:43+00:00

I have a boost::bimap<int, boost::shared_ptr<A>> container and want to return an iterator to the

  • 0

I have a boost::bimap<int, boost::shared_ptr<A>> container and want to return an iterator to the left view ensuring constness of the contents. Returning container.left.begin() returns an iterator that dereferences to std::pair<int const, boost::shared_ptr<A> const>.

Obviously, this is not what I want, since it’s possible to change A by dereferencing the shared_ptr. I want an iterator that dereferences to std::pair<int const, boost::shared_ptr<A const>> (I don’t really care if the shared_ptr is const or not). I know I should probably be doing this with the use of boost::transform_iterator, but I can’t figure out what the “casting” function should look like.

Can anybody help me out with this? Or is there another, easier method to obtain what I want?

Edit: Here’s what I have so far, suffice to say, it’s giving me a good 2 screens worth of errors.

typedef boost::bimap<unsigned int, boost::shared_ptr<A> > container_type;
typedef container_type::left_const_iterator base_const_iterator;
typedef boost::transform_iterator<makeIterConst<A>, base_const_iterator> const_iterator;

template <typename T>
struct makeIterConst : std::unary_function<std::pair<unsigned int const, boost::shared_ptr<T> const>, std::pair<unsigned int const, boost::shared_ptr<T const> const> >
{
  std::pair<unsigned int const, boost::shared_ptr<T const> const> operator() (std::pair<int const, boost::shared_ptr<T> const> const & orig) const
  {
    std::pair<int const, boost::shared_ptr<T const> const> newPair(orig.first, boost::const_pointer_cast<T const>(orig.second));
    return newPair;
  }
};

Here’s the “core” error:

note: candidate function not viable:
no known conversion from ‘const
boost::bimaps::relation::structured_pair,
boost::bimaps::tags::tagged,
boost::bimaps::relation::member_at::right>,
mpl_::na,
boost::bimaps::relation::normal_layout>’
to ‘const std::pair >’ for
1st argument

  • 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-21T17:23:44+00:00Added an answer on May 21, 2026 at 5:23 pm

    The problem is that your value_type is not actually a std::pair (and is not implicitly convertible to one), and thus cannot be passed to makeIterConst::operator().

    Make it take const base_const_iterator::value_type & instead.

    #include <iostream>
    #include <string>
    #include <boost/shared_ptr.hpp>
    #include <boost/make_shared.hpp>
    #include <boost/bimap.hpp>
    #include <boost/iterator/transform_iterator.hpp>
    
    struct A
    {
        std::string data;
        A(const std::string& s) : data(s) {}
    };
    
    typedef boost::bimap<unsigned int, boost::shared_ptr<A> > container_type;
    typedef container_type::left_map::const_iterator base_const_iterator;
    
    template <typename T>
    struct makeIterConst : std::unary_function<base_const_iterator::value_type const &,
                                               std::pair<unsigned int const, boost::shared_ptr<T const> const> >
    {
         std::pair<unsigned int const, boost::shared_ptr<T const> const> operator()
                 (base_const_iterator::value_type const & orig) const
         {
             std::pair<int const, boost::shared_ptr<T const> const> newPair(orig.first, boost::const_pointer_cast<T const>(orig.second));
             return newPair;
         }
    };
    
    typedef boost::transform_iterator<makeIterConst<A>,
                                     base_const_iterator> const_iterator;
    
    int main()
    {
        container_type m;
        boost::shared_ptr<A> p = boost::make_shared<A>("foo");
        m.insert( container_type::value_type(1, p));
    
    //  using regular iterator
        for( base_const_iterator left_iter = m.left.begin();
                             left_iter != m.left.end();
                             ++left_iter )
        {
            std::cout << left_iter->first << " --> " << left_iter->second->data << '\n';
            left_iter->second->data =  "bar"; // compiles
        }
    
        // using constified iterator
        for( const_iterator left_iter = boost::make_transform_iterator(m.left.begin(), makeIterConst<A>() );
                  left_iter != boost::make_transform_iterator(m.left.end(), makeIterConst<A>() );
                ++left_iter )
        {
            std::cout << left_iter->first << " --> " << left_iter->second->data << '\n';
    //   the following will give a compilation error, as expected
    //      left_iter->second->data =  "changed_foo";
        }
    }
    

    test: https://ideone.com/fHIUe

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

Sidebar

Related Questions

No related questions found

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.