23

gcc 4.9 allows the following code, but gcc 4.8 and clang 3.5.0 reject it.

void foo(auto c)
{
    std::cout << c.c_str();
}

I get warning: ISO C++ forbids use of 'auto' in parameter declaration [-Wpedantic] in 4.9 but in 4.8 and clang I get error: parameter declared 'auto'.

manlio
  • 18,345
  • 14
  • 76
  • 126
user4048234
  • 253
  • 2
  • 6
  • 1
    With gcc 4.9.1, `g++ -std=c++11` gives me "warning: use of ‘auto’ in parameter declaration only available with -std=c++1y or -std=gnu++1y". With `g++ -std=c++14` there's no warning. – Keith Thompson Sep 16 '14 at 23:05
  • 5
    @KeithThompson, Weird, it's definitely not part of C++14 (lambdas, yes, but not other functions). – chris Sep 16 '14 at 23:06
  • @chris: Playing around with gcc 4.9.1, [here's an example](http://codepad.org/mm1jRI4X). The compiler used by codepad.org doesn't handle it, but I get 4 lines of output with the expected size and value on each line. Apparently `foo` acts like a template. – Keith Thompson Sep 16 '14 at 23:11

2 Answers2

18

Yes, this is an extension. It's likely to be added to C++17 as part of the 'concepts' proposal, I believe.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • 9
    `It's likely to be added to C++17 as part of the 'concepts' proposal, I believe.` Indeed, known as [generic functions](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4040.pdf) ([dcl.fct]). – user657267 Sep 17 '14 at 00:16
  • 1
    @user657267 The wording used for this in the [Concepts Technical Specification](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4333.pdf) seems to be "Abbreviated Function Templates". – Jonathan Mee Jan 07 '15 at 18:05
  • 1
    @JonathanMee Good to know, I guess "generic function" was too...generic. – user657267 Jan 08 '15 at 00:25
  • Because it is an extension shouldn't it have been enabled for `gnu++14` dialect and not for `c++14`? – bolov Jan 29 '15 at 17:58
  • 2
    @bolov As I understand it, the difference between the 'standard' modes and the 'gnu' modes is that only _non-conforming_ extensions are disabled in the standard modes. Conforming extensions (that is, extensions which modify the behavior of only programs which are ill-formed or have undefined behavior under the C++ spec) are still enabled. `auto` parameters on normal functions is ill-formed under the (current) spec, so this extension is still enabled in the standard mode. To disable all extensions (as much as gcc allows) use `-Werror=pedantic`. – bames53 Jan 29 '15 at 18:48
  • 7
    A year and a half later, concepts won't be in C++17, so generic functions won't be either. – Barry Apr 15 '16 at 16:16
  • 1
    @Barry "abbreviated function templates" – T.C. Apr 16 '16 at 06:00
  • 1
    `A year and a half later, concepts won't be in C++17, so generic functions won't be either.` a very sad thing indeed. – Aluan Haddad Feb 12 '17 at 13:48
  • 1
    `A year and a half later` Concepts will be a thing in C++20. – KeyC0de Oct 04 '18 at 19:03
12

This is Concepts Lite speak for

template<class T>
void foo(T c)
{
    std::cout << c.c_str();
}

The auto just replaces the more verbose template<class T>. Similarly, you can write

void foo(Sortable c)

as a shorthand for

template<class T> 
requires Sortable<T>{}
void foo(T c)

Here, Sortable is a concept, which is implemented as a conjunction of constexpr predicates that formalize the requirements on the template parameter. Checking these requirements is done during name lookup.

In this sense, auto is a completely unconstrained template.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304