Enumerations, constructor overloads with similar conversions

Why is VisualC ++ (2008) confused? C2666: 2 overloads have similar conversions when I specify an enumeration as a second parameter, but not when I specify a bool type?

Shouldn't we introduce a match already to exclude the second constructor, because it is of type "basic_string"?

#include <string>
using namespace std;

enum EMyEnum { mbOne, mbTwo };
class test {
public: 
#if 1 // 0 = COMPILE_OK, 1 = COMPILE_FAIL
    test(basic_string<char> myString, EMyEnum myBool2) { }
    test(bool myBool, bool myBool2) { }
#else
    test(basic_string<char> myString, bool myBool2) { }
    test(bool myBool, bool myBool2) { }
#endif
};

void testme() {
    test("test", mbOne);
}

I can get around this by specifying the 'ie. basic_string & myString ', but not if it is' const basic_string & myString'.

Also called explicitly via "test ((basic_string)" test ", mbOne); also works.

I suspect this has something to do with every expression / type allowed to bool through the integral '! = 0'.

Curious for comments anyway :)

+3
1

, , -, , .

, , const char[5], std::string ( ) bool ( , bool). bool , , .

, "" :

test(basic_string<char> myString, EMyEnum myBool2) { }  // (1)
test(bool myBool, bool myBool2) { }                     // (2)

const char[5] (2) ( ). - EMyEnum (1), ; (2) ( bool).

:

test(basic_string<char> myString, bool myBool2) { }    // (3)
test(bool myBool, bool myBool2) { }                    // (4)

- (4), (3), (4). , (4), .

, , ,

test(basic_string<char>("test"), mbOne);

(1).

+5

Source: https://habr.com/ru/post/1747464/


All Articles