是否有任何gcc标志禁止隐式bool到int转换?我想用这段代码得到任何警告:
bool
int
void function( int value, bool flag ) { }int main(){ int a = 123; bool flag = true; //oops, a common mistake function( flag, a );}
void function( int value, bool flag ) { }
int main()
{
int a = 123;
bool flag = true;
//oops, a common mistake
function( flag, a );
}
a11xaf1n1#
作为解决方法,在C++11中,您可以删除其他可能的重载:
template <typename T> void function(int, T) = delete;
yyhrrdl82#
回答你的问题:不,在这种情况下没有gcc标志来发出警告。你的问题在gcc邮件列表上讨论了好几次。例如here:编译器没有检查这一点的主要原因在于,否则每个语句(如if( intval ))也会引发警告。
if( intval )
8ljdwjyq3#
在C中,你可以将一个值 Package 在一个只支持一种类型的泛型选择中:
#include <stdbool.h>#include <stdio.h>bool x = true;int y = _Generic(1, bool:2);int main(void) { printf("%d\n", y);}
#include <stdbool.h>
#include <stdio.h>
bool x = true;
int y = _Generic(1, bool:2);
int main(void) {
printf("%d\n", y);
这会出错(GCC 4.9),但如果用true或x替换1,编译时不会出错。举个例子:
true
x
1
#include <stdbool.h>void function( int value, bool flag ) { }#define function(V, F) function(V, _Generic(F, bool:F))int main() { int a = 123; bool flag = true; function( flag, a ); // error: '_Generic' selector of type 'int' is not compatible with any association}
#define function(V, F) function(V, _Generic(F, bool:F))
int main() {
function( flag, a ); // error: '_Generic' selector of type 'int' is not compatible with any association
kqlmhetl4#
clang-tidy会警告您,或者更好的是,将此设置为您的错误。这个测试是readability-implicit-bool-conversion。在早期版本的linter中,测试被命名为readability-implicit-bool-cast。
readability-implicit-bool-cast
zfciruhq5#
使用 Package 器类:
class Boolean{ bool flag;public: explicit Boolean(bool something){} bool getValue() const {return flag;} void setValue(bool a) {flag = a;}};void function(int value,Boolean flag ) { }int main(){ int a = 123; Boolean flag(true); function( flag, a ); // fails! Boolean isn't a int value :)}
class Boolean
bool flag;
public:
explicit Boolean(bool something){}
bool getValue() const {return flag;}
void setValue(bool a) {flag = a;}
};
void function(int value,Boolean flag ) { }
Boolean flag(true);
function( flag, a ); // fails! Boolean isn't a int value :)
wko9yo5t6#
使用kernel. h中question min宏中的思想,您可以使用gcc的typeof
typeof
#include <stdbool.h>#define function(x, y) do { \ __typeof(x) tmpone = (x); \ int tmptwo = 0; \ (void) (&tmpone == &tmptwo); \ fx((x), (y)); \ } while (0)void fx(int value, bool flag) { (void)value; (void)flag;}int main(void) { int a = 123; bool flag = true; function(a, flag); function(flag, a); // oops, a common mistake}
#define function(x, y) do { \
__typeof(x) tmpone = (x); \
int tmptwo = 0; \
(void) (&tmpone == &tmptwo); \
fx((x), (y)); \
} while (0)
void fx(int value, bool flag) {
(void)value;
(void)flag;
function(a, flag);
function(flag, a); // oops, a common mistake
6条答案
按热度按时间a11xaf1n1#
作为解决方法,在C++11中,您可以删除其他可能的重载:
yyhrrdl82#
回答你的问题:不,在这种情况下没有gcc标志来发出警告。你的问题在gcc邮件列表上讨论了好几次。例如here:
编译器没有检查这一点的主要原因在于,否则每个语句(如
if( intval )
)也会引发警告。8ljdwjyq3#
在C中,你可以将一个值 Package 在一个只支持一种类型的泛型选择中:
这会出错(GCC 4.9),但如果用
true
或x
替换1
,编译时不会出错。举个例子:
kqlmhetl4#
clang-tidy会警告您,或者更好的是,将此设置为您的错误。
这个测试是readability-implicit-bool-conversion。在早期版本的linter中,测试被命名为
readability-implicit-bool-cast
。zfciruhq5#
使用 Package 器类:
wko9yo5t6#
使用kernel. h中question min宏中的思想,您可以使用gcc的
typeof