简体   繁体   English

bool和C / C ++互操作性

[英]bool and C/C++ interoperability

When I write a function in C, which may also prove useful in a C++ context, shall I use the bool macro or not? 当我在C中编写一个函数时,它也可能在C ++上下文中有用,我是否应该使用bool宏? Could that lead to compatibility issues, at C and C++ both define a bool type (well, C befines _Bool, but you know what I mean :-))? 这可能导致兼容性问题,在C和C ++都定义了一个bool类型(好吧,C befines _Bool,但你知道我的意思:-))?

Why not use int ? 为什么不使用int It is always properly supported. 始终得到适当的支持。

The only reason to use bool would be if you knew for sure that your code would be used only in modern C and C++ environments. 使用bool的唯一原因是,如果您确定您的代码仅在现代C和C ++环境中使用。 But if there was any chance it would have to run on some old legacy compiler, perhaps for an obscure embedded processor, someone would have to insert macros and massage the code. 但如果有可能它必须在一些旧的遗留编译器上运行,也许对于一个不起眼的嵌入式处理器,有人必须插入宏并按摩代码。

如果您对保存内存非常感兴趣,可以使用char代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM