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

34,765

Solution 1

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 2

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 3

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*/ };
Share:
34,765
user3639557
Author by

user3639557

Updated on March 27, 2020

Comments

  • user3639557
    user3639557 over 4 years

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

    int function(auto data)
    {
        //DOES something
    }
    
  • edmz
    edmz about 9 years
    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?
  • Mike Seymour
    Mike Seymour about 9 years
    @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.
  • Ben Voigt
    Ben Voigt about 9 years
    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?
  • Mike Seymour
    Mike Seymour about 9 years
    @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.
  • Rapptz
    Rapptz about 9 years
    @BenVoigt Yeah that's the idea. Concepts Lite introduces auto for parameters as short-hand for unconstrained parameters and introduces concepts for constrained ones.
  • TartanLlama
    TartanLlama about 9 years
    Generic lambdas are a C++14 feature.
  • xtofl
    xtofl over 4 years
    It's 2019 today; the standard is mostly ready, auto is allowed as function parameter declaration. This is called 'abbreviated function template': en.cppreference.com/w/cpp/language/…
  • Yehezkel B.
    Yehezkel B. over 4 years
    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