简体   繁体   中英

C++ msvc compiler not type checking function parameter that's a reference

enum inputs : int
{
    OPTION1,
    OPTION2
};
struct optionStr
{
    float someData;
    inputs tst;
    optionStr(inputs _in) { tst = _in; }
    optionStr() {}
};
void foo2(const optionStr& _in)
{
    printf("inputs tst: %d\n", _in.tst);
}
int main()
{
    foo2(OPTION1); // no compile time error?!
}

OPTION1 is definitely not a optionStr yet no type error from msvc. If I remove the constructor optionStr(inputs _in) { tst = _in; } optionStr(inputs _in) { tst = _in; }
then I get the compile time error. Not sure what the logic is. How would I actually get this code to actually do some type checking?

Marking constructor with a single argument explicit will prevent construction of unnamed temporary in foo2 invocation.

explicit optionStr(inputs _in) { tst = _in; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM