Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
João Afonso Avatar asked May 24 '17 11:05

João Afonso


People also ask

Does clang define __ GNUC __?

Notice that clang also defines the GNU C version macros, but you should use the clang feature checking macros to detect the availability of various features. The values of the __clang_major__ , __clang_minor__ , and __clang_patchlevel__ macros are not consistent across distributions of the Clang compiler.

Is clang better than GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

How does clang work?

Clang Design: Like many other compilers design, Clang compiler has three phase: The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code. The optimizer: its goal is to do some optimization on the AST generated by the front end.

What is clang cc1?

The -cc1 argument indicates that the compiler front-end is to be used, and not the driver. The clang -cc1 functionality implements the core compiler functionality. So, simply speaking. If you do not give -cc1 then you can expect the "look&feel" of standard GCC.


1 Answers

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.

like image 68
Colen Avatar answered Sep 29 '22 06:09

Colen