将long long转换为int时,C ++编译器(gcc版本4.4.7 20120313(Red Hat 4.4.7-23.0.1)(GCC))不会发出预期的警告(请参见下面的代码示例)。
// compiler: gcc version 4.4.7 20120313 (Red Hat 4.4.7-23.0.1) (GCC)
// compiled with flags -pedantic -Wextra -Wshadow -Wall -Wconversion -std=gnu++0x
#include <stdio.h>
class AConv {
public:
long long z() {
return 7634843234L;
}
void p() {
const int v = z(); // NO WARNING. WHY ? See next line which calls the same function.
const int w = this->z(); // compiler warning: conversion to 'int' from 'long long int' may alter its value
printf( "v=%d, w=%d\n", v, w); // butchered values shown
}
};
我发现这种行为违反直觉。
它是g ++的唯一功能吗?
我想强制编译器为两个调用生成警告,而不仅仅是第二个:
v = z();
w = this->z();
我无法升级编译器(我在有约束的公司环境中工作)。
答案 0 :(得分:1)
在海湾合作委员会世界中,-Wall
不足以得到此类警告。需要-Wconversion
。
编辑:
https://wandbox.org/处的一些测试告诉我,GCC 4.4.7没有提供此警告。提供此功能的第一个版本是4.7.3。
答案 1 :(得分:0)
您需要很长的后缀'LL'。您刚刚指定了long(较小的类型)
return 7634843234LL;