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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:57:09+00:00 2026-06-10T02:57:09+00:00

Compiler: g++ 4.4.3 Boost…: 1.49.0 OS……: Ubuntu Note: It’s been 15 years since I

  • 0
Compiler: g++ 4.4.3
Boost...: 1.49.0
OS......: Ubuntu

Note: It’s been 15 years since I seriously used C++, so I’m relearning and learning new things as I also try to learn Boost.

Given the following code:

1.      class Beta {
2.      public:
3.          std::string name();
4.      }
5.      
6.      class Alpha {
7.      public:
8.          Beta m_beta;
9.      }
10.     
11.     Alpha one;

For various reasons, I wanted to use boost:bind to achieve
the same result as if “one.m_beta.name()” were called. I
thought the following would do it:

12.     boost::function<std::string(Alpha)>
13.         b = boost::bind(
14.             &Beta::name,
15.             boost::bind(&Alpha::m_beta, _1)
16.         );
17.     cout << "NAME = " << b(one) << "\n";

But when I compile (g++ 4.4.3 on Ubuntu) I get the following
error:

error: invalid conversion from ‘const Beta*’ to ‘Beta*’

After looking at the actual type definition resulting from
lines 13-16, it looks like line 15 is becoming ‘const Beta*’, but
the bind wrapping it expects ‘Beta*’.

However, this DOES work:

30.     boost::function<std::string(Beta)>
31.         p1 = boost::bind(&Beta::name,_1);
32.     boost::function<Beta(Alpha)>
33.         p2 = boost::bind(&Alpha::m_beta,_1);
34.     std::cout << "NAME = " << p1(p2(one)) << "\n";

But I really don’t want to carry around a lot of intermediate variables.
It did make me think that there must be some way to get the
first version to work as well. I’ve already tried the
following as replacements for line 15, but they all give out
one or another error at compilation time too.

50. (boost::function<Beta&(Alpha)>)(boost::bind(&Alpha::m_beta,_1))
51. (boost::function<Beta(Alpha)>)(boost::bind(&Alpha::m_beta,_1))
52. (boost::function<Beta*(Alpha)>)(boost::bind(&Alpha::m_beta,_1))
53. boost::protect(boost::bind(&Alpha::m_beta,_1))
54. boost::ref(boost::bind(&Alpha::m_beta,_1))
55. const_cast<Beta*>(boost::bind(&Alpha::m_beta,_1))
56. const_cast<boost::function<Beta(Alpha) > >(boost::bind(&Alpha::m_beta,_1))
57. const_cast<boost::function<Beta*(Alpha) > >(boost::bind(&Alpha::m_beta,_1))

What am I missing?

  • 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-10T02:57:11+00:00Added an answer on June 10, 2026 at 2:57 am

    Explicitly specify a Beta& return type for the nested boost::bind:

    boost::function< std::string( Alpha ) > b =
      boost::bind(
        &Beta::name,
        boost::bind< Beta& >( &Alpha::m_beta, _1 ) );
    

    The reason the const Beta* and Beta* conversion errors are occurring is the result of boost::bind having the same behavior as boost::mem_fn, which states:

    mem_fn also supports pointers to data members by treating them as functions taking no arguments and returning a (const) reference to the member.`

    Thus, the nested-bind is returning a const Beta&, which is an invalid instance to invoke the non-const name() member function on Beta.

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

Sidebar

Related Questions

Brief version of my question: This code crashes the compiler. pThread[0] = new boost::thread(
Compiler: clang++ x86-64 on linux. It has been a while since I have written
I am working on a platform with a gcc compiler however boost cannot compile
Background Using boost and other similar libraries is the easiest way to find compiler
I'm building the boost libraries with bjam for both the intel compiler and vs2008,
I wanted to use boost::thread in my program, but get the following compiler error
I'm new using Boost serialization. I'm using xml serialization and the non intrusive version.
I would like to install Boost library without the need of Visual Studio compiler,
I just downloaded boost 1.4.7.0, but compiler can't find boost/lexcal_cast.hpp I am trying to
I'm using Boost Spirit to parse source files in a little compiler project. If

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.