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

Warning when comparing references to bool and int with MSVC 2015

以下代码使用 MSVC(2015 更新 3)生成警告 - /W4:

const bool& a = true;
const int& b = 1;
if(a == b)

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

但没有引用它编译干净。

const bool a = true;
const int b = 1;
if(a == b)

为什么?

编辑:

刚刚也在没有 const 的情况下进行了测试

bool a = true;
int b = 1;
if(a == b)

警告再次出现...

编辑 2:

在调试中编译...我确实不得不在 const noref 情况下保持沉默 C4127: conditional expression is constant...

编辑 3:

以下是 3 种情况的拆解:

常量引用

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)  

仅常量

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)  

没有 const 没有 ref

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)  

所以似乎 if 语句有一个跳转,所以它没有优化并且在 const 情况下没有警告诊断(调试配置)...

This 是 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;
}

汇编输出:

ref(int):
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -36(%rbp)
        movl    , %eax
        movb    %al, -21(%rbp)
        leaq    -21(%rbp), %rax
        movq    %rax, -8(%rbp)
        movl    , %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(%rbp)
        movl    , -8(%rbp)
        movl    , %eax
        popq    %rbp
        ret
noref_noconst(int):
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -20(%rbp)
        movb    , -1(%rbp)
        movl    , -8(%rbp)
        movzbl  -1(%rbp), %eax
        cmpl    -8(%rbp), %eax
        sete    %al
        movzbl  %al, %eax
        popq    %rbp
        ret

这里关于程序集最容易注意的是 noref 甚至没有跳转;这只是一个简单的 return 1.

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

编辑:

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

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

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