簡體   English   中英

為什么編譯器在給定一個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 *調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM