4

I've seen 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've been searching the web to identify this syntax but I cannot. Is this legal C++ or a syntax particular to CodeFights? Can someone give me the name of this kind of initialization so that I can look it up?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Pali
  • 77
  • 7
  • I agree with @Ron:s advice above, but would likewise like to point out that in these times of instant gratification, [gamification in order to learn](https://en.wikipedia.org/wiki/Gamification_of_learning) is not necessarily always a bad thing (especially if the alternative is the lack of any studies whatsoever). For younger students, careful study (of e.g. good C++ books) combined with gamification can be a good combination. _But_, basics first! – dfrib Jul 30 '17 at 15:02

2 Answers2

3

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++.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
0

Try to compile it first and then ask!

prog.cc:1:38: error: a function-definition is not allowed here before '{' token
 int i,j,w,myFunction(auto s, auto v) {
                                      ^
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Well, it could be some exotic new syntax introduced in C++17 or beyond. Compiling the code with one compiler and one set of compiler flags doesn't prove that much. – Christian Hackl Jul 30 '17 at 14:59
  • @ChristianHackl well if this is the case, this is not standard surely. And I understood that the question reffers to this. How do you propose to improve my answer? – gsamaras Jul 30 '17 at 15:01
  • `auto` parameters seems to be part of [concepts for C++20](http://en.cppreference.com/w/cpp/language/constraints). An "unconstrained placeholder", whatever that means... – Bo Persson Jul 30 '17 at 15:03
  • Consider putting back the warnings and errors. The one you kept is the **last** error, and often the last error is an artifact that comes from trying to make sense out of the previous ones. Reading all of them clearly conveys that the compiler is saying "I have no idea what's going on here". – Pete Becker Jul 30 '17 at 16:03
  • @PeteBecker these erros where issued by [tag:c], so I posted them by mistake. [tag:C++] issues this only in Wandox. – gsamaras Jul 30 '17 at 17:31
  • @gsamaras -- well, that explains why the compiler was totally lost. – Pete Becker Jul 30 '17 at 18:53
  • @PeteBecker right! I see that my answer is 0 scored, should I delete it? – gsamaras Aug 05 '17 at 08:33