Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does a object stores lambda function have it's own address?

Based on the result of this topic

A lambda which captures no variables (nothing inside the []’s) can be converted into a function pointer

I have written a program like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void test1(){}
int main() {
    auto test2 = [](){};
    printf("%p\n%p\n", test1, &test1);
    printf("%p\n%p", test2, &test2);

    return 0;
}

the result are

0x561cac7d91a9

0x561cac7d91a9

0x7ffe9e565397

(nil)

on https://www.programiz.com/cpp-programming/online-compiler/

So the test2 is storing a function pointer to the lambda function?

and my question is that object test2, which stores the lambda data, not have its own address?

I thought this test2 should have its own address.

>Solution :

Does a object stores lambda function have it’s own address?

Yes, like all objects in C++ the variable test2 also has a unique address. You can see this by printing &test uisng cout as shown below:

int main() {
    auto test2 = [](){};
     
    std::cout << &test2;  //prints address of test2
    return 0;
}

Demo

The output of the above modified program is:

0x7ffe4d4b5fcf
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading