gpt4 book ai didi

c++ - 区分类型

转载 作者:行者123 更新时间:2023-12-01 14:48:24 25 4
gpt4 key购买 nike

我有以下代码:

#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) 两种类型,编译器有没有办法警告我?

谢谢
泰迪熊

最佳答案

Is there a way that the compiler warns me if I mix the two types type1 (unsigned char) and type2 (unsigned long)?



是的,如果你给 -Wconversion到 gcc 或 clang,或 /W3到 MSVC,他们将为您的示例打印警告。

(他们只会警告您,如果存在潜在的值(value)变化。因此,将 unsigned char 转换为 unsigned long 不会产生警告,因为此转换始终保留值(value))

关于c++ - 区分类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com