Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

c++ how to create an aggregate that store lambdas and can be passed to a function as designated initializers

$
0
0

I'm trying to pass an aggregate that stores functions as designated initializers with no success.

This is what I tried:

    typedef struct Handlers    {        std::function<void()> func1 = nullptr;        std::function<void()> func2 = nullptr;        std::function<void()> func3 = nullptr;        std::function<void()> func4 = nullptr;     } Handlers;    class Person    {    public:        Person(const char *name,                uint16_t age,                Handlers &&handlers);        void doSomething();    };

this is how i'm trying to pass the functions

int main(int argc, char *argv[]){   Person p1("Guy",        28,        {           .func1 = []() {},          .func2 = []() {},          .func3 = []() {},          .func4 = []() {}        });}

One of the error messages:

no known conversion for argument 3 from ‘<brace-enclosed initializer list>’ to ‘Handlers&&’   35 |     Person(const char *name, uint16_t age, Handlers &&handlers);

Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>