Why does a quoted string match bool method signature before a std::string?

前端 未结 3 1120
滥情空心
滥情空心 2020-12-06 05:49

Given the following methods:

// Method 1
void add(const std::string& header, bool replace);

//Method 2
void add(const std::string& name, const std::         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-06 06:12

    In your case you have has overloaded functions. Overloading resolution occurs according to Section 13.3.

    C++03 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, and
    — a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conversion sequence (13.3.3.1.3).

    Conversion pointer into bool is a standard conversion. Conversion pointer into std::string is a user-defined conversion.

    4.12 Boolean conversions An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

提交回复
热议问题