70

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

int function(auto data)
{
    //DOES something
}
Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
user3639557
  • 4,791
  • 6
  • 30
  • 55
  • 4
    Depends on what you expect it to be able to do. What do you need that a function template doesn't offer ? – Quentin Apr 29 '15 at 13:19
  • 1
    That's a horrible idea. Argument type is not only for you, but also for others to know what types your function want. Just take 3 sec and write the type, don't be that lazy (or use a template if the type is not fixed). – Synxis Apr 29 '15 at 18:49
  • Yes, with C++1z Concepts, you can do this :) – Navin Apr 30 '15 at 02:18
  • @Synxis it's not always a horrible idea. If the type is complex enough, it might be more readable to replace it with `auto` (assuming a meaningful variable name). Plus, I think `void f(auto t);` is more readable than `template void f(T t);`, assuming that `T` is not explicitly required inside `f`. – pasbi Jan 29 '19 at 22:28
  • @pasbi `assuming a meaningful variable name` This sound like string typing, and hoping for the best (and in practice, you'll always be deceived on this). I also prefer the longer form as it is more explicit about the template; maybe one day I'll change, but only after having concepts ! – Synxis Jan 30 '19 at 12:00

4 Answers4

80

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) ...

Amir Kirsh
  • 12,564
  • 41
  • 74
  • In gcc, same can be achieved with older versions of the standards (`-std=c++14` or `-std=c++17`) if you provide `-fconcepts` option. – s.yadegari Aug 31 '23 at 07:41
72

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.

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

cigien
  • 57,834
  • 11
  • 73
  • 112
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    I wonder: is it the same thing? That is, for each `T` there will be a `function` whilst for `auto` just one, as its the deduction that changes. Or perhaps I'm wrong? – edmz Apr 29 '15 at 13:50
  • 5
    @black: It's just a shorter way of writing the same thing. A different function will be instantiated for each parameter type deduced for `auto`, just as it would be for a named template parameter. – Mike Seymour Apr 29 '15 at 13:53
  • 1
    I thought implicit template parameters were supposed to be constrained by concepts... are concepts now totally dead, or is `auto` used for unconstrained arguments and (someday) concepts for constrained ones? – Ben Voigt Apr 29 '15 at 15:33
  • 1
    @BenVoigt: I've no idea, my supernatural powers are insufficient to know what C++17 will end up looking like. Concepts certainly aren't dead, and might well end up in that standard; but whether or not to allow unconstrained `auto` function parameters is somewhat orthogonal to them. – Mike Seymour Apr 29 '15 at 15:44
  • 2
    @BenVoigt Yeah that's the idea. Concepts Lite introduces `auto` for parameters as short-hand for unconstrained parameters and introduces concepts for constrained ones. – Rapptz Apr 29 '15 at 16:31
  • It's 2019 today; the standard is mostly ready, `auto` is allowed as function parameter declaration. This is called 'abbreviated function template': https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template – xtofl Dec 05 '19 at 07:11
32

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*/ };
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
-1

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

Sebastian
  • 330
  • 4
  • 9
  • 1
    As you saw, this was gcc-specific extension, and it was meant to be used only with Concepts TS. With C++20 Concepts in the standard, this becomes a standard feature – Yehezkel B. Feb 23 '20 at 05:57