[Bug c++/59823] New: conversion operator to const X& causes copy-construction of temporary

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Jan 15 11:27:00 GMT 2014


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

            Bug ID: 59823
           Summary: conversion operator to const X& causes
                    copy-construction of temporary
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org

extern "C" int puts(const char*);

struct X
{
  X() { }

  X(const X&) { puts("copy"); }
  X(X&&) { puts("move"); }

  X& operator=(const X&) { puts("copy assign"); return *this; }
  X& operator=(X&&) { puts("move assign"); return *this; }
};

struct wrap
{
  wrap(const X& x) : wrapped(&x) { }

  const X* wrapped;

  operator const X&() const { return *wrapped; }
};

int main()
{
  X x1, x2;
  wrap w(x2);
  x1 = w;
}


When compiled with -std=c++11 the expected output is:

copy assign

but G++ produces:

copy
move assign

It seems that on the last line of main() G++ incorrectly creates a
temporary, doing "x1 = X(w)" instead of "x1 = w"



More information about the Gcc-bugs mailing list