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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:19:31+00:00 2026-06-06T03:19:31+00:00

Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes…) const; and not

  • 0

Why definition of std::function<>::operator() in the C++ standard is:

R operator()(ArgTypes...) const;

and not

R operator()(ArgTypes&&...) const;

?

One would think that to correctly forward parameters, we need the && and then use std::forward<ArgTypes>... in the function body when forwarding the call?

I partially reimplemented std::function to test this and I found out that if I use the &&, I get “cannot bind ‘xxx’ lvalue to ‘xxx&&'” from g++ when I try later to pass parameters by value to operator(). I thought that I got enough grasp of the rvalue/forwarding concepts, but still I cannot grok this point. 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-06T03:19:32+00:00Added an answer on June 6, 2026 at 3:19 am

    Perfect forwarding only works when the function itself (in this case operator()) is templated and the template arguments are deduced. For std::function, you get the operator() argument types from the template parameters of the class itself, which means they’ll never be deduced from any arguments.

    The whole trick behind perfect forwarding is the template argument deduction part, which, together with reference collapsing, is what perfect forwarding is.

    I’ll just conveniently link to my other answer about std::forward here, where I explain how perfect forwarding (and std::forward) works.

    Note that std::function‘s operator() doesn’t need perfect forwarding, since the user himself decides what the parameters should be. This is also the reason why you cannot just add && to operator(); take this example:

    void foo(int){}
    
    int main(){
      // assume 'std::function' uses 'ArgTypes&&...' in 'operator()'
      std::function<void(int)> f(foo);
      // 'f's 'operator()' will be instantiated as
      // 'void operator()(int&&)'
      // which will only accept rvalues
      int i = 5;
      f(i); // error
      f(5); // OK, '5' is an rvalue
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write the global function: std::ostream& operator<<(std::ostream& out, const Str& str) {
I have this function declaration and definition.. definition void loadFromFile( string const& fileName, Frames&
I have the following definition of a boost::function object: typedef boost::function<std::string (std::string, std::string)> concat;
I'm getting two compile errors: /home/sater/projects/MotionManager/videoProcess.cpp:11:1: error: a function-definition is not allowed here before
I'm trying to get std::async to launch a function that accepts a unique_ptr with
The definition of AudioBufferList looks weird to me… i guess my C is not
In this MySQL table definition: CREATE TABLE groups ( ug_main_grp_id smallint NOT NULL default
I was perusing section 13.5 after refuting the notion that built-in operators do not
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
I have a join function that operates on STL strings. I want to be

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.