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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:49:29+00:00 2026-06-17T07:49:29+00:00

The return type of std::bind is (intentionally) unspecified. It is storable in a std::function.

  • 0

The return type of std::bind is (intentionally) unspecified. It is storable in a std::function.

The example program below shows how I have to explicitly cast the temporary object returned by std::bind() to a std::function in order to call fn1().

If the return type of std::bind was knowable, I could overload the Callback constructor & would no longer need to explicitly cast std::bind temporary objects.

Is there any way to avoid the explicit cast?

// g++ -std=c++11 test.cxx
#include <functional>

using std::placeholders::_1;

class A
{
    public:
        void funcA (int x) { }
};

class Callback
{
    public:
        Callback () = default;
        Callback (std::function<void(int)> f) { }
        // Wish we knew the return type of std::bind()
        // Callback (return_type_of_std_bind f) { }
};

void fn0 (std::function<void(int)> f) { }
void fn1 (Callback cb) { }

int main (void)
{
    A a;
    fn0(std::bind(&A::funcA, &a, _1)); // ok
    fn1(std::function<void(int)>(std::bind(&A::funcA, &a, _1))); // ok, but verbose
    fn1(std::bind(&A::funcA, &a, _1)); // concise, but won't compile
}

Probably not relevant, but I’m using gcc 4.7.2 on Linux.

  • 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-17T07:49:31+00:00Added an answer on June 17, 2026 at 7:49 am

    Best to give Callback a universal constructor:

    struct Callback
    {
        typedef std::function<void(int)> ftype;
        ftype fn_;
    
        template <typename T,
                  typename = typename std::enable_if<std::is_constructible<ftype, T>::value>::type>
        Callback(T && f) : fn_(std::forward<T>(f))
        { }
    };
    

    (I added the second, defaulted template argument to only enable this constructor for types T for which the statement makes sense, so as not to create false convertibility properties.) Note how this technique re­moves one implicit user-defined conversion from the conversion chain, by invoking an explicit con­struc­tor for fn_.

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

Sidebar

Related Questions

I have a Java code where the return type of a function has unbounded
I have a situation where a boost::function and boost::bind (actually a std::tr1::function and bind)
I'd like to bind operator new (see example below). If the constructor doesn't have
I have this as a return type in Scala Map[String, Seq[Map[String, Seq[MyClass]]]] I have
I have a thread with return type DWORD in C but it is then
I have just been working with boost::bind and boost::function and noticed the following behaviour
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Suppose I have a calculator class that implements the Strategy Pattern using std::function objects
I was trying to understand c++0x std::bind and std::function usage in event handling /
I need a function wrapper for std::bind that will be called before the function

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.