João Afonso
João Afonso

Reputation: 1934

Why does clang emit these warnings?

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.

Upvotes: 7

Views: 878

Answers (1)

Colen
Colen

Reputation: 13918

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.

Upvotes: 1

Related Questions