This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/55098] New: c++11: move constructor doesn't run at all (but with a hammer)


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

             Bug #: 55098
           Summary: c++11: move constructor doesn't run at all (but with a
                    hammer)
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: lisp2d@lisp2d.net


standard says:
A non-template constructor for class X is a move constructor if its first
parameter is of type X&&, const
X&&, volatile X&&, or const volatile X&&, and either there are no other
parameters or else all other
parameters have default arguments (8.3.6). [ Example: Y::Y(Y&&) is a move
constructor.
struct Y {
Y(const Y&);
Y(Y&&);
};
extern Y f(int);
Y d(f(1)); // calls Y(Y&&)
Y e = d; // calls Y(const Y&)

test.cpp:
#include    <iostream>
class    A{
public:
int    a;
explicit    A(int    const    &    x):a{x}{std::cout<<"A(int const
&)"<<std::endl;}
A(A    const    &    x):a{x.a}{std::cout<<"A(A const &)"<<std::endl;}
A(A    &&    x):a{x.a}{std::cout<<"A(A &&)"<<std::endl;}
A operator    +(A    const & x){std::cout<<"A+(A const &)"<<std::endl;return
A{a + x.a};}
A operator    +(A    && x){std::cout<<"A+(A &&)"<<std::endl;return A{a + x.a};}
};

extern A    f(int    const    &    x);

int    main(int,char**){
std::cout<<"---"<<std::endl;
A x(f(0));
std::cout<<"---"<<std::endl;
A    y(f(1)+f(2));
std::cout<<"---"<<std::endl;
}

A    f(int    const    &    x){
    A    tmp{x};
    ++    (tmp.a);
    return    tmp;}

result:
---
A(int const &)
---
A(int const &)
A(int const &)
A+(A &&)
A(int const &)
---

The hammer is:
A x(static_cast<A &&>(f(0)));
A y(static_cast<A &&>(f(1)+f(2)));


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]