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

Return brace-enclosed initializer list as struct

$
0
0

Giving below simplified code compiled with g++ -c test.cpp or g++ -std=c++17 -c test.cpp

#include <cstddef>

struct sd_bus_vtable {
        union {
                struct {
                        size_t element_size;
                } start;
                struct {
                        const char *member;
                        const char *signature;
                } signal;
        } x;
};

sd_bus_vtable get()
{
    return {
        .x = {
            .signal = {
                .member = "",
                .signature= "",
            }
        }
    };
}

It compiles fine on GCC 9.2.0 and clang 5/6, but fails on 8.3.0 or 7.4.0 with below error message:

test.cpp:25:5: error: could not convert ‘{{{"", ""}}}’ from ‘<brace-enclosed initializer list>’ to ‘sd_bus_vtable’
     };

To work around it, the function get() could be changed as below, but it looks not so clean...

sd_bus_vtable get()
{
    struct sd_bus_vtable t = {
        .x = {
            .signal = {
                .member = "",
                .signature= "",
            }
        }
    };
    return t;
}

The question is, is the above code valid or not? If yes, does it trigger some bug in GCC that is fixed in GCC9?


Viewing all articles
Browse latest Browse all 22055

Trending Articles



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