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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:08:08+00:00 2026-06-12T11:08:08+00:00

Consider the following code: #include <iostream> struct test { void public_test() { [this]() {

  • 0

Consider the following code:

#include <iostream>

struct test
{
    void public_test()
    {
        [this]() { private_test(); }();
    }

private:
    void private_test()
    {
        std::cout << "test\n";
    }
};

int main()
{
    test().public_test();
}

The lambda captures this and then calls a private method of the captured object. Now this code compiles and works (prints test) using VC++ 2012. Whereas this is quite intuitive and useful behaviour, I’d like to know if this is guaranteed to work by standard. Thus, does a lambda have private access to any object captured through this?

I tried to look this up in the standard reading through 5.1.2 [expr.prim.lambda] but could not really find a definite answer (being not that well-versed in the depths of the standard). The only paragraph that seemed useful to me is

The type of the lambda-expression (which is also the type of the
closure object) is a unique, unnamed nonunion class type — called the
closure type — whose properties are described below. This class type
is not an aggregate (8.5.1). The closure type is declared in the
smallest block scope, class scope, or namespace scope that contains
the corresponding lambda-expression. [ Note: This determines the set
of namespaces and classes associated with the closure type (3.4.2).
The parameter types of a lambda-declarator do not affect these
associated namespaces and classes. —end note ]

But on the other a normal local class type defined in a member function doesn’t have private access to the surrounding class. So a lambda having private access would somehow raise lambdas above mere syntactic sugar for a local function object to something more involved, since it would need additional “compiler magic” to somehow make it a friend of the surrounding class.

So does a lambda have private access to any object captured through this and if yes, which parts of the standard allow this happen?

  • 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-12T11:08:09+00:00Added an answer on June 12, 2026 at 11:08 am

    I don’t think the fact that it is a lambda is relevant. All lambda does
    is define a local class. And according to §11/2: “A local class of a
    member function may access the same names that the member function
    itself may access.”

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

Sidebar

Related Questions

Consider the following code: #include <iostream> using namespace std; class Test { static int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following code snippet: #include <iostream> using namespace std; struct Element { void
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider the following code: #include<iostream> #include<utility> struct Base { int baseint; }; struct Der1
Consider the following code: #include <algorithm> #include <iostream> #include <vector> struct A { int
Consider the following code: #include <iostream> struct X{ X(){ throw 0; } }; void

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.