71

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

int function(auto data)
{
    //DOES something
}
5
  • 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, 2015 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, 2015 at 18:49
  • Yes, with C++1z Concepts, you can do this :)
    – Navin
    Apr 30, 2015 at 2: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<typename T> void f(T t);, assuming that T is not explicitly required inside f.
    – pasbi
    Jan 29, 2019 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, 2019 at 12:00

4 Answers 4

82

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

1
  • 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 at 7: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.

6
  • 1
    I wonder: is it the same thing? That is, for each T there will be a function<T> whilst for auto just one, as its the deduction that changes. Or perhaps I'm wrong?
    – edmz
    Apr 29, 2015 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. Apr 29, 2015 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, 2015 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. Apr 29, 2015 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, 2015 at 16:31
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*/ };
1
  • 5
    Generic lambdas are a C++14 feature. Apr 30, 2015 at 7:24
-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

1
  • 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 Feb 23, 2020 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.