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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:46:18+00:00 2026-06-15T11:46:18+00:00

std::function provides a constructor from an rvalue ref. What happens to the moved function

  • 0

std::function provides a constructor from an rvalue ref.
What happens to the moved function object by standard? Will it be empty so that calling it again has no effects?

  • 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-15T11:46:19+00:00Added an answer on June 15, 2026 at 11:46 am

    Under 20.8.11.2.1p6, function(function &&f) leaves f in a valid state with an unspecified value.

    The empty state is a valid state, so you should expect that the moved-from function object can be empty.

    Because function performs type erasure, and function objects can be arbitrarily expensive, the optimisation to leave the moved-from object empty makes sense:

    std::function<void()> g{std::bind{f, std::array<int, 1000>{}}};
    std::function<void()> h{std::move{g}};
    

    After h has been constructed by move from g, one would expect the contained bind have been transferred from g to h rather than copying, so g would be left empty.

    For the following program, gcc 4.5.1 prints empty:

    #include <functional>
    #include <iostream>
    void f() {}
    int main() {
        std::function<void()> g{f}, h{std::move(g)};
        std::cout << (g ? "not empty\n" : "empty\n");
    }
    

    This is not necessarily the most optimal behaviour; inlining small callables (e.g. function pointers) creates a situation where copying the callable is more efficient than moving it and emptying the moved-from object, so another implementation could leave g in a non-empty callable state.

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

Sidebar

Related Questions

how to remove function that bound to member function of this object : std::vector<std::function<void(int)>>
Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not
While working on this question , I noticed that GCC (v4.7)'s implementation of std::function
Suppose I have a calculator class that implements the Strategy Pattern using std::function objects
One of our classes provides tr1::function callback object. When I try assigning a member
From what I understand about inheritance in C++ is that whenever the constructor of
I'm writing a class which shares several different features with std::function (or at least
Is this code correct? std::function<int(int)> f = [&f](int n) -> int { return n
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
I use the following code: struct WorkData { std::string name; std::function<void(std::string)> Callback; WorkData(){}; WorkData(const

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.