[Issue 4500] scoped moves class after calling the constructor
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jul 26 01:59:09 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4500
Max Samukha <samukha at voliacable.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |samukha at voliacable.com
--- Comment #2 from Max Samukha <samukha at 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: -------
More information about the Digitalmars-d-bugs
mailing list