在一個項目中,我在C++和使用stdbool.h定義的C庫之間進行了接口連接。與stdbool.h接口C++
#ifndef _STDBOOL_H
#define _STDBOOL_H
/* C99 Boolean types for compilers without C99 support */
/* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */
#if !defined(__cplusplus)
#if !defined(__GNUC__)
/* _Bool builtin type is included in GCC */
typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
#endif
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
#endif
#endif
某些結構有bool
成員。因此,如果我將這些結構中的一個定義爲C++函數中的局部變量並將其傳遞給C函數,則C++和C之間的大小不一致,因爲bool在C++中是再見的,而在C中是4。
有沒有人任何意見,如何克服這種不訴諸我目前的解決方案是
//#define bool _Bool
#define bool unsigned char
這違背了stdbool.h
更新:_Bool現在在VS2013中定義,所以我們還需要檢查_MSC_VER <1800。http://msdn.microsoft.com/en-us/library/hh409293.aspx – Tom 2014-12-02 15:47:24