问题
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.
回答1:
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);
回答2:
Problem is argument type of print_size
.
Your auto w = {1, 2, 3};
has type std::initializer_list<int>
and this doesn't match to a vector<T>
, there is no automatic conversion.
So you can correct print_size
this way:
template<class T>
void print_size(std::initializer_list<T> a)
{
std::cout << a.size() << '\n';
}
Or even better change how template is parametrized:
template<class T>
void print_size(const T& a)
{
std::cout << a.size() << '\n';
}
回答3:
Without C++17 deduction guides the compiler cannot deduce T
for std::vector<T>
parameter from argument of type std::initializer_list<T>
, unfortunately.
I would suggest adding an overload for print_size(std::initializer_list<T> const&)
.
回答4:
casting w would be a solution
print_size((std::vector<int>)w);
来源:https://stackoverflow.com/questions/50491107/c-error-no-matching-function-for-call-to-print-size