Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the status of abbreviated functions in C++? [duplicate]

Tags:

c++

c++17

c++14

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?

like image 207
wyer33 Avatar asked Dec 29 '25 08:12

wyer33


2 Answers

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

like image 192
Jonathan Mee Avatar answered Jan 01 '26 01:01

Jonathan Mee


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.

like image 20
Tommy Andersen Avatar answered Jan 01 '26 00:01

Tommy Andersen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!