Is there a way to pass auto as an argument in C++?

C++FunctionArgumentsAuto

C++ Problem Overview


Is there a way to pass auto as an argument to another function?

int function(auto data)
{
    //DOES something
}

C++ Solutions


Solution 1 - C++

If you want that to mean that you can pass any type to the function, make it a template:

template <typename T> int function(T data);

There's a proposal for C++17 to allow the syntax you used (as C++14 already does for generic lambdas), but it's not standard yet.

Edit: C++ 2020 now supports auto function parameters. See Amir's answer below

Solution 2 - C++

C++20 allows auto as function parameter type

This code is valid using C++20:

int function(auto data) {
   // do something, there is no constraint on data
}

As an abbreviated function template.

This is a special case of a non constraining type-constraint (i.e. unconstrained auto parameter). Using concepts, the constraining type-constraint version (i.e. constrained auto parameter) would be for example:

void function(const Sortable auto& data) {
    // do something that requires data to be Sortable
    // assuming there is a concept named Sortable
}

The wording in the spec, with the help of my friend Yehezkel Bernat:

> 9.2.8.5 Placeholder type specifiers [dcl.spec.auto] > > placeholder-type-specifier: > >> type-constraintopt auto > >> type-constraintopt decltype ( auto ) > >>> 1. A placeholder-type-specifier designates a > placeholder type that will be replaced later by deduction from an > initializer.

>>> 2. A placeholder-type-specifier of the form > type-constraintopt auto can be used in the decl-specifier-seq of a > parameter-declaration of a function declaration or lambda-expression > and signifies that the function is an abbreviated function template > (9.3.3.5) ...

Solution 3 - C++

Templates are the way you do this with normal functions:

template <typename T>
int function(T data)
{
    //DOES something
}

Alternatively, you could use a lambda:

auto function = [] (auto data) { /*DOES something*/ };

Solution 4 - C++

I dont know when it changed, but currently syntax from Question is possible with c++14:

https://coliru.stacked-crooked.com/a/93ab03e88f745b6c

There is only warning about it:

> g++ -std=c++14 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp:5:15: warning: use of 'auto' in parameter declaration only available with -fconcepts void function(auto data)

With c++11 there is an error:

> main.cpp:5:15: error: use of 'auto' in parameter declaration only available with -std=c++14 or -std=gnu++14

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3639557View Question on Stackoverflow
Solution 1 - C++Mike SeymourView Answer on Stackoverflow
Solution 2 - C++Amir KirshView Answer on Stackoverflow
Solution 3 - C++TartanLlamaView Answer on Stackoverflow
Solution 4 - C++SebastianView Answer on Stackoverflow