• Create Account

### #ActualCornstalks

Posted 03 May 2012 - 08:14 AM

The type of a lambda function is a unique anonymous functor type for every lambda function. Two separate lambdas that have the exact same definition in the exact same scope will have two separate types. As such, trying to get at the exact type of a lambda function is an exercise in futility. Just store the std::function by value.

With that in mind, it made me curious if
auto lines = []() { cout<<"- - - - - -"<<endl; };
auto dots  = []() { cout<<". . . . . ."<<endl; };
lines = dots;

works. I don't have a compiler on my current machine to test if the assignment gives a compile time error. if no one answers this before I get to a proper computer and can test this myself, I'll just edit this with what I found.

Got that sooner than I thought I would. For my little test, the type of lines is const main()::<lambda()>& and the assignment generates a compile time error.

However, a normal function pointer worked:
#include <iostream>
#include <functional>

typedef void (*function) ();

int main()
{
function lines = []() { std::cout << " - - - - - " << std::endl; };
auto dots = []() { std::cout << " . . . . . " << std::endl; };

lines = dots;

lines();
dots();
}


But yes, I would just go with a std::function.

### #2Cornstalks

Posted 03 May 2012 - 08:13 AM

The type of a lambda function is a unique anonymous functor type for every lambda function. Two separate lambdas that have the exact same definition in the exact same scope will have two separate types. As such, trying to get at the exact type of a lambda function is an exercise in futility. Just store the std::function by value.

With that in mind, it made me curious if
auto lines = []() { cout<<"- - - - - -"<<endl; };
auto dots  = []() { cout<<". . . . . ."<<endl; };
lines = dots;

works. I don't have a compiler on my current machine to test if the assignment gives a compile time error. if no one answers this before I get to a proper computer and can test this myself, I'll just edit this with what I found.

Got that sooner than I thought I would. For my little test, the type of lines is const main()::<lambda()>& and the assignment generates a compile time error.

However, this worked:
#include <iostream>
#include <functional>

typedef void (*function) ();

int main()
{
function lines = []() { std::cout << " - - - - - " << std::endl; };
auto dots = []() { std::cout << " . . . . . " << std::endl; };

lines = dots;

lines();
dots();
}


But yes, I would just go with a std::function.

### #1Cornstalks

Posted 03 May 2012 - 07:59 AM

The type of a lambda function is a unique anonymous functor type for every lambda function. Two separate lambdas that have the exact same definition in the exact same scope will have two separate types. As such, trying to get at the exact type of a lambda function is an exercise in futility. Just store the std::function by value.

With that in mind, it made me curious if
auto lines = []() { cout<<"- - - - - -"<<endl; };
auto dots  = []() { cout<<". . . . . ."<<endl; };
lines = dots;

works. I don't have a compiler on my current machine to test if the assignment gives a compile time error. if no one answers this before I get to a proper computer and can test this myself, I'll just edit this with what I found.

PARTNERS