简体   繁体   中英

C++ Compound literal

In C i can do this:

ppackage ppnull() {
    return (ppackage) {
        .type = NULL
    }
}

However, in C++ I get syntax errors. I use the GNU g++ compiler. Is there a switch to enable this?

With c++11 you can use initializer list:

struct ppackage
{
    void* type;
};

ppackage ppnull()
{
    return {nullptr};
}

Or just

ppackage ppnull()
{
    return {};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM