Enums, Constructor overloads with similar conversions.

Posted by David Thornley on Stack Overflow See other posts from Stack Overflow or by David Thornley
Published on 2010-05-28T01:27:06Z Indexed on 2010/05/28 1:31 UTC
Read the original article Hit count: 303

Why does VisualC++ (2008) get confused 'C2666: 2 overloads have similar conversions' when I specify an enum as the second parameter, but not when I define a bool type?

Shouldn't type matching already rule out the second constructor because it is of a 'basic_string' type?

#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 work around this by specifying a reference 'ie. basic_string &myString' but not if it is 'const basic_string &myString'.

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

I suspect this has something to do with every expression/type being resolved to a bool via an inherent '!=0'.

Curious for comments all the same :)

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-c++