C++ msvc 编译器不对引用的函数参数进行类型检查
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
绝对不是 optionStr
但 msvc 没有类型错误。
如果我删除构造函数
optionStr(inputs _in) { tst = _in; }
然后我得到编译时错误。不确定逻辑是什么。我实际上如何让这段代码真正进行一些类型检查?
用单个参数标记构造函数 explicit
将阻止在 foo2
调用中构造未命名的临时变量。
explicit optionStr(inputs _in) { tst = _in; }
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
绝对不是 optionStr
但 msvc 没有类型错误。
如果我删除构造函数
optionStr(inputs _in) { tst = _in; }
然后我得到编译时错误。不确定逻辑是什么。我实际上如何让这段代码真正进行一些类型检查?
用单个参数标记构造函数 explicit
将阻止在 foo2
调用中构造未命名的临时变量。
explicit optionStr(inputs _in) { tst = _in; }