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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:43:43+00:00 2026-06-01T17:43:43+00:00

Does the standard define what happens with this code? #include <iostream> template <typename Func>

  • 0

Does the standard define what happens with this code?

#include <iostream>

template <typename Func>
void callfunc(Func f)
{
   ::std::cout << "In callfunc.\n";
    f();
}

template <typename Func>
void callfuncref(Func &f)
{
   ::std::cout << "In callfuncref.\n";
    f();
}

int main()
{
    int n = 10;
    // n is captured by value, and the lambda expression is mutable so
    // modifications to n are allowed inside the lambda block.
    auto foo = [n]() mutable -> void {
       ::std::cout << "Before increment n == " << n << '\n';
       ++n;
       ::std::cout << "After increment n == " << n << '\n';
    };
    callfunc(foo);
    callfunc(foo);
    callfuncref(foo);
    callfunc(foo);
    return 0;
}

The output of this with g++ is:

$ ./a.out 
In callfunc.
Before increment n == 10
After increment n == 11
In callfunc.
Before increment n == 10
After increment n == 11
In callfuncref.
Before increment n == 10
After increment n == 11
In callfunc.
Before increment n == 11
After increment n == 12

Are all features of this output required by the standard?

In particular it appears that if a copy of the lambda object is made, all of the captured values are also copied. But if the lambda object is passed by reference none of the captured values are copied. And no copies are made of a captured value just before the function is called, so mutations to the captured value are otherwise preserved between calls.

  • 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-01T17:43:44+00:00Added an answer on June 1, 2026 at 5:43 pm

    The type of the lambda is simply a class (n3290 §5.1.2/3), with an operator() which executes the body (/5), and an implicit copy constructor (/19), and capturing a variable by copy is equivalent to copy-initialize (/21) it to a non-static data member (/14) of this class, and each use of that variable is replaced by the corresponding data member (/17). After this transformation, the lambda expression becomes only an instance of this class, and the general rules of C++ follows.

    That means, your code shall work in the same way as:

    int main()
    {
        int n = 10;
    
        class __Foo__           // §5.1.2/3
        {
            int __n__;          // §5.1.2/14
        public:
            void operator()()   // §5.1.2/5
            {
                std::cout << "Before increment n == " << __n__ << '\n';
                ++ __n__;       // §5.1.2/17
                std::cout << "After increment n == " << __n__ << '\n';
            }
            __Foo__() = delete;
            __Foo__(int n) : __n__(n) {}
          //__Foo__(const __Foo__&) = default;  // §5.1.2/19
        }
        foo {n};                // §5.1.2/21
    
        callfunc(foo);
        callfunc(foo);
        callfuncref(foo);
        callfunc(foo);
    }
    

    And it is obvious what callfuncref does here.

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

Sidebar

Related Questions

Does the C++ Standard Library define this function, or do I have to resort
Can I safely expect this #define TEMPLATE_DECL_BEGIN_0 template < #define TEMPLATE_DECL_BEGIN_1 TEMPLATE_DECL_BEGIN_0 typename Arg0
I have a template member function with this signature: template<typename T> void sync(void (*work)(T*),
Why does the Standard define end() as one past the end, instead of at
What is the standard encoding of C++ source code? Does the C++ standard even
The standard logging code does not seem to work instantiating a logger with the
Does the HTTP standard or something define which encoding should be used on special
suppose I have a header foo.h like this: #ifndef FOO_H #define FOO_H #include <string>
Does the standard define precisely what I can do with an object once it
What does standard specify about indexing sequence with Attr objects inside NamedNodeMap object? I

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.