The code may become legal C++20. Here's why...
One of the quirks of the C declaration heritage in C++ is that multiple declarations can go on one line.
int a, b, c;
As you know, you can add pointers and references to the mix, retaining the "basic" type:
int a, *b, &c = x;
It is also legal to extend this syntactic quirk to function declarations. The following declares f
as a function returning an int
:
int a, b, c, f();
Given an appropriate context, you can even define the function on the same line:
struct S {
int a, b, c, f() { return 0; }
};
And of course, you can add parameters to the function:
struct S {
int a, b, c, f(float x, double y) { return x + y; }
};
The final step is to turn those parameter types into auto
, which C++20 may allow as part of the concepts proposal, a feature originally planned for C++17.
GCC already supports this syntax as an extension. Here is a complete example:
#include <iostream>
struct S {
int a, b, c, f(auto x, auto y) { return x + y; }
};
int main()
{
S s;
std::cout << s.f(1.0, 2.0) << '\n';
}
This means that while the code is semi-correct or will likely be correct in the future, the comments are not, because i
, j
and w
are not local variables, and they are not initialised to 0.
It is also most certainly not a "typical" use of C++.