Defining a return variable in the function declaration. Is this legitimate in modern C++?

  • A+

I am a big fan of CodeSignal, but I found a strange piece of C++ grammar (the first line):

string r, longestDigitsPrefix(string s) {    for(auto const c : s)    {       if(isdigit(c))         r += c;       else         break;    }    return r; } 

Is defining string r, in the function declaration valid in modern C++?

The above code compiles and passes all tests in the CodeSignal console, but it produced a compiler error when I tried to compile locally (--std=c++14).

If this is valid grammar in modern C++ and which standard revision does it comply with?


Yeah, C++ grammar is weird. Basically, when it comes to declarations (and only declarations), we have this thing where:

T D1, D2, ... ,Dn; 

means ([dcl.dcl]/3):

T D1; T D2; ... T Dn; 

This will be familiar in the normal cases:

int a, b; // declares two ints 

And probably in the cases you've been told to worry about:

int* a, b, *c; // a and c are pointers to int, b is just an int 

But declarators can introduce other things too:

int *a, b[10], (*c)[10], d(int); 

Here a is a pointer to int, b is an array of 10 ints, c is a pointer to an array of 10 ints, and d is a function taking an int returning an int.

However, this only applies to declarations. So this:

string r, longestDigitsPrefix(string s); 

is a valid C++ declaration that declares r to be a string and longestDigitsPrefix to be a function taking a string and returning a string.

But this:

string r, longestDigitsPrefix(string s) { return s; } 

is invalid C++. Function definitions have their own grammar and cannot appear as part of the init-declarator-list.

The definition of that function is also bad, since it's using a global variable to keep track of state. So even if it were valid, longestDigitsPrefix("12c") would return "12" the first time but "1212" the second time...


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: