[Bug c++/85043] New: -Wuseless-cast false positive for temporary objects

bugs@michael-lossin.de gcc-bugzilla@gcc.gnu.org
Thu Mar 22 18:31:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85043

            Bug ID: 85043
           Summary: -Wuseless-cast false positive for temporary objects
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugs@michael-lossin.de
  Target Milestone: ---

> cat x.cc
#include <iostream>

struct A
{
    int value = 42;
    A & inc() { ++value; return *this; }
};

int main()
{
  {
    A a;
    std::cout << A(a).inc().value << ", ";
    std::cout << a.value << "\n";
  }
  {
    A a;
    std::cout << a.inc().value << ", ";
    std::cout << a.value << "\n";
  }
}

> g++ -Wuseless-cast -o x x.cc
x.cc: In function 'int main()':
x.cc:13:21: warning: useless cast to type 'A' [-Wuseless-cast]
     std::cout << A(a).inc().value << ", ";
                     ^

> ./x
43, 42
43, 43


Here, A(a) is not a cast but creates a temporary object which in turn is
modified. Removing the "useless cast" would alter the original object, so the
warning should not be shown in this case.


More information about the Gcc-bugs mailing list