Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is gcc or clang correct about this behavior?

I have a little toy program:

static int value = 0;  int function(int &value=value) {     return value; }  int main() {     function(); } 

Compiling with g++ 7.2:

g++ -std=c++11 -Wall -Wextra test.cc -o test

No problem.

Compiling with clang++-3.9:

clang++-3.9 -std=c++11 -Wall -Wextra test.cc -o test

test.cc:3:25: error: default argument references parameter 'value' int function(int &value=value) {                         ^~~~~ test.cc:8:5: error: no matching function for call to 'function'     function();     ^~~~~~~~ test.cc:3:5: note: candidate function not viable: requires single argument 'value', but no arguments were provided int function(int &value=value) {     ^ 2 errors generated. 

Kaboom. Who's right?

like image 324
gct Avatar asked Feb 13 '18 16:02

gct


People also ask

Should I use Clang or 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.

Is Clang replacing GCC?

Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA, and HIP frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.

What is Clang used for?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code. Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP.

Does Clang use GCC?

decided to write the frontend Clang of C, C++, and Objective-C languages from scratch to completely replace GCC. As the name suggests, Clang only supports C, C++, and Objective-C. Development started in 2007 and the C compiler was first completed.


1 Answers

I think clang is correct. From basic.scope.pdecl:

The point of declaration for a name is immediately after its complete declarator (Clause [dcl.decl]) and before its initializer (if any), except as noted below. [ Example:

int x = 12;{ int x = x; } 

Here the second x is initialized with its own (indeterminate) value. — end example ]

Also, from dcl.fct.default:

Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default argument, even if they are not evaluated. Parameters of a function declared before a default argument are in scope and can hide namespace and class member names

like image 143
R Sahu Avatar answered Oct 11 '22 18:10

R Sahu