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

            Bug ID: 95911
           Summary: [8/9/10/11] returning && makes an error without any
                    warning
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 570070308 at qq dot com
  Target Milestone: ---

Created attachment 48789
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48789&action=edit
the .ii file

class A
{
public:
    A()
    {
        printf("A create.\n");
    }
    A(const A&a)
    {
        printf("A copy create.\n");
    }
    A(A&&a)
    {
        printf("A move create.\n");
    }
    ~A()
    {
        printf("A delete.\n");
    }
};
A newA()
{
    A a;
    return a;
}
A&& bug(A&& x)
{
    printf("bug\n");
    return std::move(x);
}
int main()
{
    A &&a=newA();              //ok
    printf("----------\n");
    A &&b=bug(newA());         //error
    printf("----------\n");
    A c=bug(newA());           //ok
    printf("----------\n");
    return 0;
}


runing the code, the result is:
A create.
----------
A create.
bug
A delete.
----------
A create.
bug
A move create.
A delete.
----------
A delete.
A delete.


In main function create three class A veriables, but only two class A veriables
are deleted at last. But the compiler do not give any warning. I have try -Wall
-Wextra but it still have no warning. I have tried with
-fno-elide-constructors, -O3 and -O0, the result is the same. In my
comprehension of c++, the second class A veriable should not be deleted. When
double using function bug(see the .ii file), you can find it was not deleted so
early. If it should be deleted, that means the code is dangerous and compiler
should give a warning. There is no warning in g++ 8/9/10/11, and also no
warning in clang++.

Reply via email to