C ++ local variables in the list of return arguments (as shown in CodeFights)

I saw this form of syntax on CodeFights.com for C ++ functions:

int i,j,w,myFunction(auto s, auto v) {
  // here, i, j,and w are local variable, apparently initialized to 0 
  // all work done inside this function is typical, legal C++
  return [some int value];
}

I searched the Internet to identify this syntax, but I cannot. Is this legal C ++ or syntax for CodeFights? Can someone give me a name for this initialization so that I can view it?

+4
source share
2 answers

Code can become legal C ++ 20. That's why ...

One of the quirks of the legacy of the C declaration in C ++ is that multiple declarations can go on the same line.

int a, b, c;

As you know, you can add pointers and links to the mix, preserving the "base" type:

int a, *b, &c = x;

. f , int:

int a, b, c, f();

, :

struct S {
    int a, b, c, f() { return 0; }
};

, , :

struct S {
    int a, b, c, f(float x, double y) { return x + y; }
};

, auto, ++ 20 , , ++ 17.

GCC . :

#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';
}

, , , , , , i, j w , 0.

, , "" ++.

+3

, !

prog.cc:1:38: error: a function-definition is not allowed here before '{' token
 int i,j,w,myFunction(auto s, auto v) {
                                      ^
0

Source: https://habr.com/ru/post/1682616/


All Articles