c++ - 为什么它首先调用 bool_function?

标签 c++

我对下面的代码很好奇,有人可以解释为什么它首先调用 bool func 吗? “str”不是更适合 arg 类型的字符串吗?

 void a(bool input)
    {
        cout<<"I amd first"<<endl;
        cout<<input<<endl;
    }

    void a(const string &input)
    {
        cout<<"I amd second"<<endl;
        cout<<input<<endl;
    }

    int main( )
    {
        a("str");  //  call  void a(bool input)

        a(string("str"));   //call  void a(const string &input)

        return 0; 
    }

最佳答案

"str"const char[4] 类型,它会立即衰减到 const char *,以及从任何指针的转换bool 的类型被认为是 before 自定义类型的非显式构造函数。

所以,我会说答案是“因为标准是这么说的”。

相关段落应该是13.3.3.2 ¶2:

When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)

  • a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence [...]

关于c++ - 为什么它首先调用 bool_function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554136/

相关文章:

c++ - 如何仅构建 boost 所需的模块?

c++ - Arduino 崩溃并在 "random points"处重新启动

c++ - 逻辑流程不正确?获取数独游戏坐标的函数

c++ - Opencv中的去噪

c++ - 对数组的困惑

c++ - 级联文件加载错误

c++ - 在 C++ 中如何更改存储在字符串中的文件扩展名?

c++ - STL 的 multimap 如何插入尊重排序?

c++ - 为什么 sublime text 和 VSCode 不显示运行时错误?

c# - 基于原生OpenCV的unity上的Android应用