Search code examples
c++c++11initializer-listauto

C++ error: no matching function for call to 'print_size'


I have this code:

#include <iostream>
#include <vector>

template<typename T>
void print_size(std::vector<T> a)
{
    std::cout << a.size() << '\n';
}

int main()
{
    std::vector<int> v {1, 2, 3};
    print_size(v);

    auto w = {1, 2, 3};
    // print_size(w); // error: no matching function for call to 'print_size'
                      // candidate template ignored: could not match 'vector' against 'initializer_list'
}

...which compiles and runs without any issues. But if I enable the commented-out line, it produces the error no matching function for call to 'print_size'.

I would like to know what is the correct way to write this code in C++11 and later versions.


Solution

  • For auto w = {1, 2, 3}; the type of w will be std::initializer_list<int>, and print_size(w); fails because template parameter T can't be deduced; template argument deduction does not consider implicit conversions.

    Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

    You can specify template argument explicitly,

    print_size<int>(w);
    

    Or you can make w to be a std::vector<int> instead; and if you persist using auto you need to specify the type explicitly.

    auto w = std::vector<int>{1, 2, 3};
    print_size(w);