7

The clang compiler emit warnings for the snippet below, as can be seen here.

clang++ -std=c++14 -O0 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp:1:18: warning: braces around scalar initializer [-Wbraced-scalar-init]
void point(int = {1}, int = {2}) {}
                 ^~~
main.cpp:1:29: warning: braces around scalar initializer [-Wbraced-scalar-init]
void point(int = {1}, int = {2}) {}
                            ^~~

2 warnings generated.

Why is this?

void point(int = {1}, int = {2}) {}

int main(){
    point();
}

As far as I can tell, {1} and {2} are perfectly valid default arguments according to [dcl.fct.default]/1, [dcl.fct]/3 and [dcl.init]/1.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
João Afonso
  • 1,934
  • 13
  • 19
  • 6
    Well, it's only a warning, IMO this just means "hey, that's useless and looks weird, are you sure that's what you meant?". – Quentin May 24 '17 at 11:41
  • @Quentin As this appears to be a perfectly valid code, there should no warning. – João Afonso May 24 '17 at 11:42
  • 7
    There are plenty valid snippets that trigger warnings -- invalid code triggers errors. `if(x = something());` is another one. – Quentin May 24 '17 at 11:44
  • @JoãoAfonso if this is valid but misleading (or dangerous) the compiler can generate a warning (cf misleading indentation gcc warning) – nefas May 24 '17 at 11:44
  • @Quentin But there is no warning from clang for this `int i = {1};` – João Afonso May 24 '17 at 11:54
  • Might be the same reasoning from [this answer](https://stackoverflow.com/a/3462768/4342498) – NathanOliver May 24 '17 at 11:59
  • @NathanOliver Not anymore. GCC doesn't emit a warning for `int i = {1};` . – João Afonso May 24 '17 at 12:03
  • @JoãoAfonso Maybe they used to have that warning as well, and when they removed it for a normal variable initialization they forgot to do the same for default arguments. – Barmar May 30 '17 at 20:25
  • Possible duplicate of [gcc warning: braces around scalar initializer](https://stackoverflow.com/questions/3462513/gcc-warning-braces-around-scalar-initializer) – AMA Jun 09 '17 at 14:22
  • @João Afonso: "As this appears to be a perfectly valid code, there should no warning" - this is a rather strange thing to say. *Warnings* are diagnostic messages that are *typically* issued for *perfectly valid code* (well-formed code), which the compiler sees as "valid but suspicious". *Invalid* (ill-formed) code normally triggers *errors*, not warnings. Not every compiler follows this distinction precisely, but nevertheless that's the idea. – AnT stands with Russia Jun 15 '17 at 02:21

1 Answers1

1

Braces are typically used when initializing instances of structs, for example:

struct example {
  int member1;
  int member2;
};

example x = { 1, 2 };

Clang is telling you that your use of braces isn't "normal" for initializing a single value. This warning could help if you weren't familiar with the syntax for initializing values in C++, or perhaps if the types had previously been structs before a refactoring of some sort.

You can either stop using braces when initializing integers, or pass the -Wno-braced-scalar-init flag to the compiler to stop it reporting the warning.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Colen
  • 13,428
  • 21
  • 78
  • 107