vendredi 28 avril 2017

Can the C++ struct aggregate initialisation be used to create temporary instance?

To quickly initialise small structs I often use the aggregate initialisation to keep the code small and simple:

struct Foo {
    int a;
    int b;
    int c;
};
Foo temp = {1, 2, 3};

I would assume this should also work to create a temporary instance which can be passed as function argument:

void myFunc(Foo foo) {
    // ...
}

Is this possible? How is the exact syntax to use this feature?

myFunc({1, 2, 3}); // ???
myFunc(Foo {1, 2, 3}); // ???

Aucun commentaire:

Enregistrer un commentaire