为什么编译器在给定 const char * 作为模板化参数的类型时选择 bool 而不是 string_view?
why does compiler choose bool over string_view when given a const char * as the type of the templated argument?
#include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
当我用 const char *
调用更新时,编译器用 bool
参数而不是 string_view
调用函数?!为什么??!
从 const char *
到 std::string_view
的转换(通过 constructor of std::string_view
) is a user-defined conversion; which is a worse match than the standard conversion (the implicit conversion from const char*
to bool
) in overload resolution.
1) A standard conversion sequence is always better than a user-defined conversion sequence or an ellipsis conversion sequence.
#include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
当我用 const char *
调用更新时,编译器用 bool
参数而不是 string_view
调用函数?!为什么??!
从 const char *
到 std::string_view
的转换(通过 constructor of std::string_view
) is a user-defined conversion; which is a worse match than the standard conversion (the implicit conversion from const char*
to bool
) in overload resolution.
1) A standard conversion sequence is always better than a user-defined conversion sequence or an ellipsis conversion sequence.