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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:04:42+00:00 2026-06-04T00:04:42+00:00

I just write an simple example to test boost::bind. I use it to instantiate

  • 0

I just write an simple example to test boost::bind. I use it to instantiate a template member function, but it won’t compile with g++ 4.6.0. I don’t know what’s the problem.
Here’s the code:

#include <boost/bind.hpp>

struct Functor
{
  void operator()()
  {

  }
};

struct DerivedFinishAction
{
  DerivedFinishAction()
  {}

  void Inc()
  {

  }

  template <typename T>
  void TmplFunc(T t)
  {
    (boost::bind(&DerivedFinishAction::BindFunc<T>, this , t))();
  }

  template <typename T>
  void BindFunc(T t)
  {
    t();
  }

  void Func()
  {
    Functor f;
    TmplFunc(f); // this is OK
    TmplFunc(boost::bind(&DerivedFinishAction::Inc, this)); // compile error
  }
};

int main(int argc, char *argv[])
{

  return 0;
}

And g++ gives the following errors:

In file included from /usr/include/boost/bind.hpp:22:0,
                 from testBind.cpp:1:
/usr/include/boost/bind/bind.hpp: In member function ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::_mfi::mf1<void, DerivedFinishAction, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, A = boost::_bi::list0, A1 = boost::_bi::value<DerivedFinishAction*>, A2 = boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > >]’:
/usr/include/boost/bind/bind_template.hpp:20:59:   instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = boost::_mfi::mf1<void, DerivedFinishAction, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, L = boost::_bi::list2<boost::_bi::value<DerivedFinishAction*>, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, boost::_bi::bind_t<R, F, L>::result_type = void]’
testBind.cpp:24:5:   instantiated from ‘void DerivedFinishAction::TmplFunc(T) [with T = boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > >]’
testBind.cpp:37:58:   instantiated from here
/usr/include/boost/bind/bind.hpp:313:9: error: invalid use of void expression

Can anybody help explain this? Why the first instantiation is OK while the second one causes compile error?

  • 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-04T00:04:43+00:00Added an answer on June 4, 2026 at 12:04 am

    There’s a (non-obvious) feature of boost::bind involved here. http://www.boost.org/libs/bind/#nested_binds

    If you write:

    void func1(int len) {return len+1;};
    int func2(std::string str) {return str.length();};
    
    assert(
        boost::bind(func1, boost::bind(func2, _1) )("Hello")
        == 6 );
    

    boost::bind assumes that what you meant is “run func2 on "Hello", then run func1 on the result”. This allows more interesting partial function application.

    In your program, you have an expression which amounts to:

    boost::bind(&DerivedFinishAction::BindFunc<...>, 
                this, 
                boost::bind(&DerivedFinishAction::Inc, this))
    

    So boost::bind tries to figure out how to run DerivedFinishAction::Inc on it arguments, so it can pass that result into DerivedFinishAction::BindFunc<...>. But DerivedFinishAction::Inc returns void, which cannot be passed into DerivedFinishAction::BindFunc<...>. Thus you get a compiler error:

    /usr/include/boost/bind/bind.hpp:313:9: error: invalid use of void expression
    

    Edit: Per the documentation, you can use protect to achieve your desired behavior:

    #include <boost/bind/protect.hpp>
    ...
    TmplFunc(boost::protect(boost::bind(&DerivedFinishedAction::Inc, this))); // no longer an error
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone provide just a simple example of how to use wxSockets write, because
how can I write just a simple disassembler for linux from scratches? Are there
Just wanted to write some recursion but can't check if the child is in
I am just starting to write ruby rails. I wrote a controller but is
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x
I'm trying to just make a simple asyncore example where one socket is the
I'm just a newer to JavaScript. I want to write a JS template class
I'm trying to write a simple smoke test for a web application. The application
I use NUNIT and mostly Test with ValueSource to do assertions. But i never
I want to write a simple HTMLUnit test script for Jenkins (former Hudson). INFO:

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.