Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, does the scope of a named parameter include the expression for its default value?

Example: Is this legal C++14?

#include <iostream>

static int d() {
    return 42;
}

static int e(int d = d()) {
    return d;
}

int main() {
    std::cout << e() << " " << e(-1) << std::endl;
}

g++ 5.4 with -std=c++14 likes it, but clang++ 3.8 with -std=c++14 complains:

samename.cxx:3:23: error: called object type 'int' is not a function or function pointer
static int e(int d = d()) {return d;}
                     ~^
like image 468
not-a-user Avatar asked Feb 17 '17 11:02

not-a-user


2 Answers

From basic.scope.pdecl/1:

The point of declaration for a name is immediately after its complete declarator and before its initializer (if any), except as noted below.

Example:

unsigned char x = 12;
{ unsigned char x = x; }

Here the second x is initialized with its own (indeterminate) value.

Clang is consistent with this section of the standard. GCC only seems to apply it in {block}.

Given:

constexpr int d() {return 42;}

The following fails in Clang, but works in GCC:

static int e(int d = d()) {return d;}
             // This ^^d should refer to the new name that shadows ::d()

The following fails both in Clang and GCC:

void func(){
    int d = d();
         // ^^d here refers to the new name that shadowed ::d();
 }
like image 127
WhiZTiM Avatar answered Oct 29 '22 12:10

WhiZTiM


It seems it is not legal: see the C++14 specification (section 3.3.2 - Point of declaration).

int x = 5;
{ int x = x; } // second x is initialized with its own undetermined state
{ int x[x]; }  // declares an array with 5 elements, since the declaration of 
               // second x is not complete when the first x is used

In your case, the declaration of d is complete, so when you use d() you're referring to the variable, not the function.

like image 37
cbuchart Avatar answered Oct 29 '22 12:10

cbuchart