What is the status of shorthand functions in C ++?

What is the status of shorthand functions in C ++? Searching around, I see some mentions of this in a working draft for C ++ concepts. At the same time, GCC seems to have no problem with code like

#include <iostream>

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

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

      

Now if I compile with -Wpedantic

I get a 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 the acronym functions are not fully standard-compliant. So what is their current status with regard to the C ++ standard and for generic C ++ compilers?

+3


source to share


2 answers


I think you are asking about shorthand function templates . The document you are linking defines it like this:

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

The meaning of this example would look like this:

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

      



The C ++ 14 standard gets generic lambdas . Example: auto lambda = [](auto x, auto y) {return x + y;};

But I didn't see anything saying that the "generic lambda" functionality would be extended to traditional functions.

Unlike Technical Report 1 , Technical Report 2 will be released as separate technical specifications: Will TR2 be released in C ++ 17?

It looks like the next hurdle for the Concepts technical specification is the WG21 Concepts Encounter .

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

+5


source


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

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

      



This will expand at compile time, how should the compiler know which type auto

should be interpreted as? In your example, you are using two different types.

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

+3


source







All Articles