Why does the compiler choose bool over string for implicit typecast of L“”?

后端 未结 5 2163
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 19:12

Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 19:35

    To simplify a bit, the following code

    #include 
    using namespace std;
    
    void f(const string &s)
    {  cout << "string version called" << endl;  }
    
    void f(const bool &b)
    {  cout << "bool version called" << endl;  }
    
    int main()
    {  f("Hello World");  }
    

    prints "bool version called". Are you sure that your code fails only with the empty string?

提交回复
热议问题