3

What's the status of abbreviated functions in C++? Searching around, I see some mention of it in a working draft on C++ concepts. At the same time, GCC seems to have no problems with code like

#include <iostream>

auto foo(auto x) {
    std::cout << x << std::endl;
}

int main() {
    foo(1);
    foo(1.2);
}

Now, if I do compile with -Wpedantic, I get the warning:

g++ -std=c++14 -Wpedantic test08.cpp -o test08
test08.cpp:3:9: warning: ISO C++ forbids use of 'auto' in parameter declaration [-Wpedantic]
 auto foo(auto x) {
         ^

which tells me that abbreviate functions are not fully in the standard. As such, what's their current status with respect to the C++ standard and with respect to common C++ compilers?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
wyer33
  • 6,060
  • 4
  • 23
  • 53
  • No there's no such thing in the C++14 standard. You can even check for yourself in [a draft of the standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf). – Some programmer dude Jan 07 '15 at 12:07
  • 4
    This will be added by the Concepts TS. Notice, that it will also allow you to write stuff like `auto foo(std::vector const& v)` - after all, the rules for template argument deduction and `auto` are the same - modulo initializer lists. – Andy Prowl Jan 07 '15 at 12:09
  • It seems like you answered your own question: they are not in the current standard, but are in the concepts proposal. – interjay Jan 07 '15 at 12:14
  • 1
    There used to be a close reason "too localized", and this question should be closed for that reason. There is no definitive answer that can be accepted; the "current status" can vary with each WG21 meeting. – MSalters Jan 07 '15 at 12:44
  • 1
    @MSalters: I don't know if that's entirely fair. Basically, I'm trying to get a feel if this is something that was just an extension in GCC or if it's slated to be in the C++17 or later standard. Yes, things can change, but I'm not sure where things are currently at. – wyer33 Jan 07 '15 at 14:35

2 Answers2

5

I think what you're asking about is Abbreviated Function Templates. The document that you link to defines it this way:

If the auto type-specifier appears in a parameter type of a function declaration, the function declaration declares an abbreviated function template. Example: void f(const auto&, int);

Meaning that example would translate to:

template <typename T>
void f(const T&, int);

The C++14 standard is getting generic lambdas. Example: auto lambda = [](auto x, auto y) {return x + y;}; But I haven't seen anything saying that "generic lambda" functionality will be extended to traditional functions.

Unlike Technical Report 1, Technical Report 2 will be released as seperate technical specifications: Is TR2 Going to be Released in C++17?

It appears that the next hurdle for the Concepts Technical Specification in particular is the WG21 Concepts Meeting.

You can read a bit more about the Concepts Technical Specification here: http://en.wikipedia.org/wiki/Concepts_%28C%2B%2B%29

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 2
    Hurdle passed, it's out for the ballot – Cubbi Jan 31 '15 at 05:09
  • 1
    @Cubbi Thanks so much for the update! I was looking at the Array Extensions Technical Specification [here](http://stackoverflow.com/a/28094952/2642059) Is there any chance you have knowledge about that one's status as well? – Jonathan Mee Feb 01 '15 at 03:36
3

It looks like you are trying to create a templated function, well at least it looks like you need one :)

template <typename X>
void foo(X x) {
    std::cout << x << std::endl;
}

This will expand during compile time, how should the compiler know which type auto should be interpreted as? In your example you use two different types.

Note though that you are not returning anything inside your function, but still uses auto as return type.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
  • 3
    The compiler would know the type just like it does when you use a polymorphic lambda: by translating `foo()` into a function template :) – Andy Prowl Jan 07 '15 at 12:10
  • 4
    Yes, the abbreviated function template in the question is equivalent to that template (`auto` being deduced just like a template parameter, as it already is in C++14 polymorphic lambdas). But you haven't answered the question: what's their current status with respect to the C++ standard and with respect to common C++ compilers? – Mike Seymour Jan 07 '15 at 12:16
  • 1
    I must have missed that part :) as I read the current draft: https://github.com/cplusplus/concepts-ts/blob/master/html/concepts.pdf it is still a long way from being ready, so C++17 at the earliest. But honestly I'm sure some one else has a more precise answer :) – Tommy Andersen Jan 07 '15 at 12:30