[英]Destinguish between types
我有以下代码:
#include <iostream>
#include <string>
typedef unsigned char type1;
typedef unsigned long type2;
type1 f(type1 value)
{
return value * 2;
}
int main()
{
type1 value1 = 1;
type2 value2 = 2000;
int int1 = f(value1);
int int2 = f(value2); // here I would expect that the compiler warns me that I mix type1 and type2
std::cout << int1 << std::endl;
std::cout << int2 << std::endl;
}
如果我混合了 type1 (unsigned char) 和 type2 (unsigned long) 两种类型,编译器有没有办法警告我?
谢谢泰迪
如果我混合了 type1 (unsigned char) 和 type2 (unsigned long) 两种类型,编译器有没有办法警告我?
是的,如果您将-Wconversion
给 gcc 或 clang,或将/W3
给 MSVC,他们将为您的示例打印警告。
(他们只会警告你,如果有潜在的价值变化。因此,将unsigned char
转换为unsigned long
不会产生警告,因为这种转换总是保留价值)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.