Given the following C++ source code 

void g();
void h();

void f( int a, int b)
{
        if (b = 1)      // case 1 - fixed constant on rhs. g() always executed.
                g();
        if (b = a)      // case 2 - variable on rhs. h() might be executed.
                h();
}

then GNU C++ 4.2 snapshot says

[EMAIL PROTECTED]:~/C++/Alphasrc> ~/gnu/42-20060114/results/bin/g++ -c -Wall 
jan15b.cc
jan15b.cc: In function 'void f(int, int)':
jan15b.cc:6: warning: suggest parentheses around assignment used as truth value
jan15b.cc:8: warning: suggest parentheses around assignment used as truth value
[EMAIL PROTECTED]:~/C++/Alphasrc>

We can see the compiler fails to distinguish case 1 and case 2.

Suggest enhance the compiler to say something different for case 1.
Fixed constant on rhs is much more likely to be a programmer error, IMHO.

Here is Intel C++ 9.0 doing what I want

jan15b.cc(6): warning #187: use of "=" where "==" may have been intended
        if (b = 1)
            ^


-- 
           Summary: constant on rhs of conditional assignment
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dcb314 at hotmail dot com
  GCC host triplet: x86_64-suse-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25808

Reply via email to