将bool和int的引用与MSVC 2015进行比较时发出警告

onq*_*tam 15 c++ compiler-warnings visual-c++

以下代码使用MSVC(2015 Update 3)生成警告 - 包含/W4:

const bool& a = true;
const int& b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)

C4805: '==': unsafe mix of type 'const bool' and type 'const int' in operation

但没有参考,它编译干净.

const bool a = true;
const int b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)

为什么?

编辑:

刚刚测试没有const

bool a = true;
int b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)

警告再次出现......

编辑2:

在Debug中编译...虽然我确实需要C4127: conditional expression is constant在const noref情况下保持沉默......

编辑3:

以下是3种情况的拆卸:

const ref

0113BA92  in          al,dx  
0113BA93  sub         esp,24h  
0113BA96  mov         eax,0CCCCCCCCh  
0113BA9B  mov         dword ptr [ebp-24h],eax  
0113BA9E  mov         dword ptr [ebp-20h],eax  
0113BAA1  mov         dword ptr [ebp-1Ch],eax  
0113BAA4  mov         dword ptr [ebp-18h],eax  
0113BAA7  mov         dword ptr [b],eax  
0113BAAA  mov         dword ptr [ebp-10h],eax  
0113BAAD  mov         dword ptr [ebp-0Ch],eax  
0113BAB0  mov         dword ptr [ebp-8],eax  
0113BAB3  mov         dword ptr [a],eax  
    const bool& a = true;
0113BAB6  mov         byte ptr [ebp-9],1  
0113BABA  lea         eax,[ebp-9]  
0113BABD  mov         dword ptr [a],eax  
    const int& b = 1;
0113BAC0  mov         dword ptr [ebp-1Ch],1  
0113BAC7  lea         ecx,[ebp-1Ch]  
0113BACA  mov         dword ptr [b],ecx  
    if(a == b)
0113BACD  mov         edx,dword ptr [a]  
0113BAD0  movzx       eax,byte ptr [edx]  
0113BAD3  mov         ecx,dword ptr [b]  
0113BAD6  cmp         eax,dword ptr [ecx]  
0113BAD8  jne         DOCTEST_ANON_FUNC_2+5Fh (0113BAEFh)  
        throw 5;
0113BADA  mov         dword ptr [ebp-24h],5  
0113BAE1  push        offset __TI1H (0117318Ch)  
0113BAE6  lea         edx,[ebp-24h]  
0113BAE9  push        edx  
0113BAEA  call        __CxxThrowException@8 (01164B04h)  
Run Code Online (Sandbox Code Playgroud)

只有const

0137BA92  in          al,dx  
0137BA93  sub         esp,0Ch  
0137BA96  mov         dword ptr [ebp-0Ch],0CCCCCCCCh  
0137BA9D  mov         dword ptr [b],0CCCCCCCCh  
0137BAA4  mov         dword ptr [ebp-4],0CCCCCCCCh  
    const bool a = true;
0137BAAB  mov         byte ptr [a],1  
    const int b = 1;
0137BAAF  mov         dword ptr [b],1  
    if(a == b)
0137BAB6  mov         eax,1  
0137BABB  test        eax,eax  
0137BABD  je          DOCTEST_ANON_FUNC_2+44h (0137BAD4h)  
        throw 5;
0137BABF  mov         dword ptr [ebp-0Ch],5  
0137BAC6  push        offset __TI1H (013B318Ch)  
0137BACB  lea         ecx,[ebp-0Ch]  
0137BACE  push        ecx  
0137BACF  call        __CxxThrowException@8 (013A4B04h)  
Run Code Online (Sandbox Code Playgroud)

没有const没有参考

0012BA92  in          al,dx  
0012BA93  sub         esp,0Ch  
0012BA96  mov         dword ptr [ebp-0Ch],0CCCCCCCCh  
0012BA9D  mov         dword ptr [b],0CCCCCCCCh  
0012BAA4  mov         dword ptr [ebp-4],0CCCCCCCCh  
    bool a = true;
0012BAAB  mov         byte ptr [a],1  
    int b = 1;
0012BAAF  mov         dword ptr [b],1  
    if(a == b)
0012BAB6  movzx       eax,byte ptr [a]  
0012BABA  cmp         eax,dword ptr [b]  
0012BABD  jne         DOCTEST_ANON_FUNC_2+44h (012BAD4h)  
        throw 5;
0012BABF  mov         dword ptr [ebp-0Ch],5  
0012BAC6  push        offset __TI1H (016318Ch)  
0012BACB  lea         ecx,[ebp-0Ch]  
0012BACE  push        ecx  
0012BACF  call        __CxxThrowException@8 (0154B04h)  
Run Code Online (Sandbox Code Playgroud)

所以似乎有一个if语句的跳转,所以它没有被优化,并且在const情况下没有警告诊断(debug config)...

Yam*_*vic 3

以下代码在 GCC 6.1 中会发生这种情况:

int ref(int num) {
   const bool& a = true;
   const int& b = 1;
   return a == b;
}

int noref(int num) {
   const bool a = true;
   const int b = 1;
   return a == b;
}


int noref_noconst(int num) {
   bool a = true;
   int b = 1;
   return a == b;
}
Run Code Online (Sandbox Code Playgroud)

装配输出:

ref(int):
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -36(%rbp)
        movl    $1, %eax
        movb    %al, -21(%rbp)
        leaq    -21(%rbp), %rax
        movq    %rax, -8(%rbp)
        movl    $1, %eax
        movl    %eax, -20(%rbp)
        leaq    -20(%rbp), %rax
        movq    %rax, -16(%rbp)
        movq    -8(%rbp), %rax
        movzbl  (%rax), %eax
        movzbl  %al, %edx
        movq    -16(%rbp), %rax
        movl    (%rax), %eax
        cmpl    %eax, %edx
        sete    %al
        movzbl  %al, %eax
        popq    %rbp
        ret
noref(int):
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -20(%rbp)
        movb    $1, -1(%rbp)
        movl    $1, -8(%rbp)
        movl    $1, %eax
        popq    %rbp
        ret
noref_noconst(int):
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -20(%rbp)
        movb    $1, -1(%rbp)
        movl    $1, -8(%rbp)
        movzbl  -1(%rbp), %eax
        cmpl    -8(%rbp), %eax
        sete    %al
        movzbl  %al, %eax
        popq    %rbp
        ret
Run Code Online (Sandbox Code Playgroud)

关于装配,这里最需要注意的一点是,在noref没有跳转的情况下;这只是一个简单的return 1

同样的事情也可能发生在 MSVC 中,它会绕过警告检测机制,而您只是没有收到警告。

编辑:

让我们看看您的程序集,特别是仅 const版本:

    if(a == b)
0137BAB6  mov         eax,1  
0137BABB  test        eax,eax  
0137BABD  je          DOCTEST_ANON_FUNC_2+44h (0137BAD4h)  
Run Code Online (Sandbox Code Playgroud)

if虽然语句 ( )确实je存在,但我们可以看到它实际上并没有比较变量——这是触发警告的精确操作。它只是将 1 放入寄存器中,然后将该寄存器与自身进行比较。