[英]Why does the compiler choose bool over string for implicit typecast of L“”?
[英]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 *
調用update時,編譯string_view
bool
參數調用函數而不是string_view
為什么??!
從const char *
到std::string_view
的轉換(通過std::string_view
的構造std::string_view
)是用戶定義的轉換; 在重載 bool
中,這是一個比標准轉換(從const char*
到bool
的隱式轉換)更糟糕的匹配。
1)標准轉換序列總是優於用戶定義的轉換序列或省略號轉換序列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.