Search code examples
c++functiong++compiler-warningsauto

Warning: `auto` in formal parameter


I executed the following program -

#include <iostream>
#include <vector>
using namespace std;

void display(const vector<auto> &arr) {
    for (auto const &val: arr) 
        cout<<val<<" ";
    cout<<endl;
}

int main() {
    vector<int> a (6);
    display(a);

    vector<double> b (3);
    display(b);
    return 0;
}

And it gives the following warning (without any error) -

warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
5 | void display(const vector<auto> &arr) {                                                                                                                          
  |                           ^~~~          

Why am I getting this warning & what is this warning about ?

Should I be using auto in as formal parameter here ??

What can be the alternative way if it's the wrong approach ??


Solution

  • You really want a template:

    template<typename T>
    void display(const vector<T>& arr)
    {
        for (auto const &val: arr) 
            cout<<val<<" ";
        cout<<endl;
    }