0

Consider this piece of code:

struct S {
    float b;
    int a;    
};

int main() {
    S s{{{}}};
    return s.a;
}

Godbolt

Clang 6.0.0 compiles this code, but shows a warning:

<source> warning: too many braces around scalar initializer [-Wmany-braces-around-scalar-init]

GCC 8.2 doesn't compile this code and reports an error:

<source>: In function 'int main()': <source>:9:10: error: braces around scalar initializer for type 'float'

Which one is correct? What does the specification say about this?

0

2 Answers 2

1

Both compilers are correct. Unless you violate a rule that says no diagnostic required the compiler should issue you a message. Whether that message is a warning or an error is up to the implementation. Normally you'll get a warning if it is something the compiler can still proceed with and an error when there is no way the compiler can continue.

Sign up to request clarification or add additional context in comments.

Comments

1

What does the specification say about this?

dcl.init.aggr/3:

3 When an aggregate is initialized by an initializer list as specified in [dcl.init.list], the elements of the initializer list are taken as initializers for the elements of the aggregate. The explicitly initialized elements of the aggregate are determined as follows:

3.1 If the initializer list is a designated-initializer-list, the aggregate shall be of class type, the identifier in each designator shall name a direct non-static data member of the class, and the explicitly initialized elements of the aggregate are the elements that are, or contain, those members.

3.2 If the initializer list is an initializer-list, the explicitly initialized elements of the aggregate are the first n elements of the aggregate, where n is the number of elements in the initializer list.

3.3 Otherwise, the initializer list must be {}, and there are no explicitly initialized elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.