use of auto in function parameters

My throw-away book says I can do this :

 
  auto f( auto n ) { return 2 * n; }

but g++ gives this error :
error: ISO C++ forbids use of ‘auto’ in parameter declaration [-Wpedantic]

Pls help
Thanks
It is forbidden in current versions of C++. Expect this feature to be available in C++20.

auto f( auto n ) { return 2 * n; }
Is shorthand for
template <typename T> auto f( T n ) { return 2 * n; }

IINM, this particular feature has been considered several times in the last decade. It's possible the author expected it to be included in whichever revision of the standard was in progress at the time of writing.
Last edited on
It's possible the author expected it to be included in whichever revision of the standard was in progress at the time of writing.

Alas, I consider that to be lazy writing.

It is one thing to point out a likely future syntax, but as far as teaching someone to use a language it is rather unhelpful to present invalid syntax in any capacity.
Last edited on
template <typename T> auto f( T n ) { return 2 * n; } // works

Thanks !
Y'all could actually save three bytes by using T instead of auto for the return value too...
Maybe, but it's not necessarily the same. For example, 2 * T{} is int when T is short.
Last edited on
Heh, true that.
Topic archived. No new replies allowed.