Search code examples
c++visual-c++typechecking

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; }
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?


Solution

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

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