簡體   English   中英

區分類型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM