2012-05-14 15 views
0

结合这是一个例子:为什么使用stdbool.h原因警告时-Wtraditional转换

#include <stdbool.h> 

void foo(bool b){}; 
void bar(bool b) {foo(b);} 

int main() { 
    bar(false); 
} 

我编译:

gcc -Wtraditional-conversion test.c 

我得到这些警告:

test.c: In function 'bar': 
test.c:4: warning: passing argument 1 of 'foo' with different width due to prototype 
test.c: In function 'main': 
test.c:7: warning: passing argument 1 of 'bar' with different width due to prototype 

为什么会发生这些警告?据我可以看到参数都是相同的类型,所以应该是相同的宽度。什么是 - 传统转换在这段非常简单的代码中引起这些警告?

我从使用我自己的bool typedef切换到stdbool.h def时开始出现这些错误。

我原来的清晰度是:

typedef enum {false, true} bool; 
+3

'-Wtraditional-conversion'意思是*如果原型导致类型转换不同于**中缺少原型***时发生的同一个参数会发生的类型转换,则发出警告。你似乎在使用C99,那么为什么你需要警告? – cnicutar

+0

你有什么版本的gcc? –

+0

@JensGustedt gcc(Gentoo 4.4.3-r2 p1.2)4.4.3 – SimonAlfie

回答

1

这是一个不理解编译器警告标志的情况。

使用-Wconversion而不是-Wtraditional-conversion可以获得警告,提醒您关于隐式转换。 -Wtraditional-conversion用于在没有原型的情况下警告转换。

因为typdef enum创建了一个默认的整数bool类型(通常为32位),因此stdbool.h将bool定义为8位,这与C++ bool兼容。

0

呼叫的警告bar是正确的,因为你问的编译器是pendantic。 false扩大为int常数0,所以它不是bool(或_Bool)。

第一个警告是一个错误。

相关问题