Thread overview
[Issue 4500] New: scoped moves class after calling the constructor
Jul 24, 2010
Christian Kamm
Jul 26, 2010
Max Samukha
Jul 26, 2010
Max Samukha
Mar 24, 2011
Kenji Hara
Mar 24, 2011
Kenji Hara
Mar 26, 2011
Kenji Hara
Jul 17, 2011
Kenji Hara
Sep 08, 2011
Walter Bright
July 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500

           Summary: scoped moves class after calling the constructor
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: andrei@metalanguage.com
        ReportedBy: kamm-removethis@incasoftware.de


--- Comment #0 from Christian Kamm <kamm-removethis@incasoftware.de> 2010-07-23 23:59:57 PDT ---
As far as I remember a class is not supposed to move.

class A {
  this() { a = this; }
  this(int i) { a = this; }
  A a;
  void check() { writeln(this is a); }
}

void main()
{
    auto a1 = scoped!A;
    a1.check(); // fails

    auto a2 = scoped!A(1);
    a2.check(); // fails

    a1.a = a1;
    a1.check(); // ok now
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 25, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED


--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-07-25 09:48:34 PDT ---
Thanks for the catch! This is a serious problem indeed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500


Max Samukha <samukha@voliacable.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha@voliacable.com


--- Comment #2 from Max Samukha <samukha@voliacable.com> 2010-07-26 01:59:01 PDT ---
I didn't know D structs were allowed to be moved without calling the copy constructor. Is it really the case?

FWIW, modern C++ implementations would optimize the copy out. For example, the following C++ test passes with a recent GNU C++:

template <typename T>
class Scoped
{
    char data[sizeof(T)];

public:
    T* payload() { return reinterpret_cast<T*>(data); }

    Scoped(int i) { new(payload()) T(i); }

    ~Scoped() { payload()->~T(); }
};

template <typename T>
Scoped<T> scoped(int i)
{
    Scoped<T> s(i);
    return s;
}

class A {
public:
    A() { a = this; }
    A(int i) { a = this; }
    A *a;
    void check() { std::cout << (this == a ? "true" : "false") << std::endl; }
    ~A() { std::cout << "~A dtor" << std::endl; }
};

int main(int argc, char *argv[])
{
    Scoped<A> a2 = scoped<A>(1);
    a2.payload()->check(); // ok
}

Anyway, it is not reasonable to base a fundamental feature like 'scoped' on RVO without having the latter in the language specification.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500



--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-07-26 03:19:53 PDT ---
The language specifies that returning a stack-allocated object by value from a function does not invoke the constructor. Currently the compiler is not up to the specification.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500



--- Comment #4 from Max Samukha <samukha@voliacable.com> 2010-07-26 04:01:35 PDT ---
But does it specify that the object should not be moved? Currently, the struct is constructed on the callee's frame and then blitted (moved) to the caller's frame, though the compiler can optimize the blit out (as GNU C++ does). If this kind of RVO is not specified, obviously 'scoped' cannot rely on it and will remain broken even if dmd will eventually be able to apply the optimization.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4500



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2011-03-23 18:02:28 PDT ---
Created an attachment (id=933)
Fixed scoped

I tried to fix this issue.
It succeeded, but resulting code includes ugly hack.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4500


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg@gmail.com


--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2011-03-23 18:11:08 PDT ---
(In reply to comment #5)
> Created an attachment (id=933) [details]
> Fixed scoped
> 
> I tried to fix this issue.
> It succeeded, but resulting code includes ugly hack.

I worked on 64bit Windows 7.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4500



--- Comment #7 from Kenji Hara <k.hara.pg@gmail.com> 2011-03-26 00:38:09 PDT ---
I posted issue 5777 and attached experimental dmd patch.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4500


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #8 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-17 06:54:56 PDT ---
https://github.com/D-Programming-Language/phobos/pull/148

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 08, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4500


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------