Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda black magic taking different types

Tags:

c++

lambda

auto

Trying to find yet another way to simply serialize my code, I had the stupid idea to try this impossible thing. But it worked. I really wonder why:

template <typename C>
void f(C c)
{
    int a = 1;
    float b = 1.5f;
    c(a);
    c(b);
}

int main()
{
    f([](auto v){
        std::cerr << v << "\n";
    });
    return 0;
}

I looked at the resulting assembly (g++-9.3 -O0 -g -S -fverbose-asm test.cpp -o test.s), and it appears that two distincts lambdas are generated: one taking a float, and the other one taking an int. Is this black magic? Is there anyone who knows the standard and can explain more about this?

like image 675
etham Avatar asked Nov 15 '20 17:11

etham


People also ask

Can a lambda have multiple destinations?

For each execution status such as Success or Failure you can choose one of four destinations: another Lambda function, SNS, SQS, or EventBridge. Lambda can also be configured to route different execution results to different destinations.

Can one lambda invoke another?

In order to allow the ParentFunction to call the ChildFunction, we need to provide the ParentFunction with specific rights to call another lambda function. This can be done by adding specific policies to a role and then assign that role to the lambda function.

Is Java good for AWS Lambda?

Java. Java has been in service for decades and is, to this day, a reliable option when choosing the backbone of your stack. With AWS Lambda is no different as it makes a strong candidate for your functions.

What are the different types of black magic?

Types of Black Magic - Voodoo, Curses, Programs, Spirits.. With all these types of black magic there is only one sure thing: the person who created the black magic in order to harm others will also receive what he created. If you wish me to remove all these types of black magic, please contact me here.

How to spell the name of a black magician?

By spelling the name: Black magic which is applied to the named person or by calling the name of person.in this method of black magician the black magician/ evil person called the name of that person with his/her father and mother name, place where he/she living, house number, and the dress he/she wore at that time.

What is black magic and how does it work?

Black magic is the use of supernatural power for selfish and evil motives.This is applied to the human being, animals, vehicles and even on plants also.This is not an easily curable method it has a long effect on the individual. This is of various type

How many forms of magic are there in Black Clover?

There are 17 Forms of Magic overall in Black Clover and each is used in notably different manners. Updated on October 29, 2021, by Reyadh Rahaman: The mages who use the various kinds of Magic in the Black Clover universe are all as diverse as the spells they cast, each with unique prowess over their chosen Forms.


Video Answer


2 Answers

A generic lambda is mostly similar to

struct lambda
{
    template <typename T>
    auto operator()(T v) const
    {
        std::cerr << v << "\n";
    }
// ...
};

The lambda itself is not a template, but its member operator() is.

like image 196
Jarod42 Avatar answered Oct 10 '22 04:10

Jarod42


This call:

f([](auto v){
        std::cerr << v << "\n";
    });

is passing a generic lambda to f, as the argument c. A generic lambda is one that has a templated member operator(), because of the auto parameter.

So when you make the following calls:

c(a);  // int argument
c(b);  // float argument

the compiler will instantiate one version of the lambda's member operator() with int, and one version of the member operator() with float.

like image 6
cigien Avatar answered Oct 10 '22 04:10

cigien