Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to use previous function parameter to declare new one?

The following code compiles cleanly with GCC:

void func(int arg1, decltype(arg1) arg2) {     (void)arg2; } int main(){} 

I used this command to compile:

g++ -std=c++14 test.cpp -o test -pedantic-errors -Wall -Wextra 

But such usage of a parameter in the middle of function declaration seems weird. Is it actually valid in standard C++, or is it a GCC extension?

like image 523
Ruslan Avatar asked Mar 01 '16 13:03

Ruslan


1 Answers

This is fine. The ISO C++11 Standard even gives your situation as an example.

First the parameter is in scope:

3.3.3 Block scope [ basic.scope.local ]

2 The potential scope of a function parameter name (including one appearing in a lambda-declarator) or of a function-local predefined variable in a function definition (8.4) begins at its point of declaration.

An example can be found here:

8.3.5 Functions [ dcl.fct ]

5 [ Note: This transformation does not affect the types of the parameters. For example, int(*)(const int p, decltype(p)*) and int(*)(int, const int*) are identical types. — end note ]

like image 96
Galik Avatar answered Oct 20 '22 17:10

Galik