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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:52:43+00:00 2026-05-19T21:52:43+00:00

I want to pass an rvalue through std::bind to a function that takes an

  • 0

I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can’t figure out how to do it. For example:

#include <utility>
#include <functional>

template<class Type>
void foo(Type &&value)
{
    Type new_object = std::forward<Type>(value);    // move-construct if possible
}

class Movable
{
public:
    Movable(Movable &&) = default;
    Movable &operator=(Movable &&) = default;
};

int main()
{
    auto f = std::bind(foo<Movable>, Movable());
    f();    // error, but want the same effect as foo(Movable())
}
  • 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-19T21:52:44+00:00Added an answer on May 19, 2026 at 9:52 pm

    The reason this fails is because when you specify foo<Movable>, the function you’re binding to is:

    void foo(Movable&&) // *must* be an rvalue
    {
    }
    

    However, the value passed by std::bind will not be an rvalue, but an lvalue (stored as a member somewhere in the resulting bind functor). That, is the generated functor is akin to:

    struct your_bind
    {
        your_bind(Movable arg0) :
        arg0(arg0)
        {}
    
        void operator()()
        {
            foo<int>(arg0); // lvalue!
        }
    
        Movable arg0;
    };
    

    Constructed as your_bind(Movable()). So you can see this fails because Movable&& cannot bind to Movable.†

    A simple solution might be this instead:

    auto f = std::bind(foo<Movable&>, Movable());
    

    Because now the function you’re calling is:

    void foo(Movable& /* conceptually, this was Movable& &&
                            and collapsed to Movable& */)
    {
    }
    

    And the call works fine (and, of course, you could make that foo<const Movable&> if desired). But an interesting question is if we can get your original bind to work, and we can via:

    auto f = std::bind(foo<Movable>,
                std::bind(static_cast<Movable&&(&)(Movable&)>(std::move<Movable&>),
                    Movable()));
    

    That is, we just std::move the argument before we make the call, so it can bind. But yikes, that’s ugly. The cast is required because std::move is an overloaded function, so we have to specify which overload we want by casting to the desired type, eliminating the other options.

    It actually wouldn’t be so bad if std::move wasn’t overloaded, as if we had something like:

    Movable&& my_special_move(Movable& x)
    {
        return std::move(x);
    }
    
    
    auto f = std::bind(foo<Movable>, std::bind(my_special_move, Movable()));
    

    Which is much simpler. But unless you have such a function laying around, I think it’s clear you probably just want to specify a more explicit template argument.


    † This is different than calling the function without an explicit template argument, because explicitly specifying it removes the possibility for it to be deduced. (T&&, where T is a template parameter, can be deduced to anything, if you let it be.)

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

Sidebar

Related Questions

want to pass boost::bind to a method expecting a plain function pointer (same signature).
I want to pass an integer value to a form in .Net so that
I want to pass in the tType of a class to a function, and
I want to pass a variable to a UIButton action, for example NSString *string=@one;
I want to pass a byte[] to a method takes a IntPtr Parameter in
Basically I want to pass a string which contains Spanish text that could be
When using call_user_func_array() I want to pass a parameter by reference. How would I
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
I want to pass a fairly generic set of data through a WCF method.
I want to pass back data from a UIDatePicker that is setup like this:

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.