Clang and GCC has an inconsistent interpretation to the case where the parameter name appears as an unevaluated operand

Viewed 76
#include <iostream>
void fun(int a = sizeof(a)){
    std::cout<< a<<std::endl;
}
int main(){
   fun();
}

Consider this case. Clang accepts it while GCC rejects it. According to [dcl.fct.default] p9

A default argument is evaluated each time the function is called with no argument for the corresponding parameter. A parameter shall not appear as a potentially-evaluated expression in a default argument.

[basic.def.odr] p2

An expression or conversion is potentially evaluated unless it is an unevaluated operand

sizeof is not a potentially-evaluated expression and a can be found at this point. I wonder which one is correct for this case?

1 Answers

GCC is wrong. It assumes that a is not yet in scope in the default argument, although generally the point of declaration is immediately after the declarator before the initializer and none of the exceptions apply here. [basic.scope.pdecl]/1

An older bug report for this is here.

Note however, that the bug report has equivalent code to yours, but claims it should be ill-formed. That is because it was written before CWG 2082 which allowed the use of a parameter in an unevaluated context in the default argument.

Related question regarding this GCC bug here.

Related