[C++0x] "canonical" class - copy/move construction/assignment

Started by
15 comments, last by DevFred 13 years, 12 months ago
It's also good news in that draft that if you needed to define the move operators, it would look like this:

Foo(Foo&&) = default;Foo& operator=(Foo&&) = default;
Advertisement
Quote:Original post by SiCrane
Yeah, and according to the C++ standard, you can use export with templates.

There is no C++0x standard yet, and the VS Support is experimental. I expect VS to eventually catch up. It's not like automatically generating move constructors and move assignment operators takes 100 man-years ;-)

Quote:Original post by SiCrane
Saying that you should write your code as if your compiler implements the language "correctly" in face of the fact that your compiler doesn't implement the language that way is just idiotic.

Koen ultimately wants to know how to write classes in the future when C++0x is actually real, right? It's not about getting the full power of rvalue references with current tools:
Quote:
I thought it was about time to start learning about them. Rvalue references will probably have the largest impact on how one should write classes.

There's no good reason to deliberately write less performant code just to satisfy some ivory tower intellectual exercise about how code "should" be written when the increasingly misnamed C++0x standard is finally delivered. Maybe in five years not defining your move constructors will be good advice. However, it doesn't take a hundred man years to manually write out a move constructor either.
Quote:Original post by SiCrane
Saying that you should write your code as if your compiler implements the language "correctly" in face of the fact that your compiler doesn't implement the language that way is just idiotic.

Not necessarily. If you're working with a large codebase, it's always nice to be able to upgrade your tools without too much effort. Living by the future rules of your compiler - currently not enforced - would be a wise thing to do. (This particular example is the other way around, of course: it's about an optimization that's not there yet).
In one of the above links it is claimed that MS employees call VC10 "the new VC6". Where I work, people still have nightmares about migrating code from VC6 to 7 and 8. Fortunately I was not around in those days :-)
Quote:Original post by DevFred
Koen ultimately wants to know how to write classes in the future when C++0x is actually real, right? It's not about getting the full power of rvalue references with current tools:

Yes. And before this thread, I believed the two to be the same ;-)
Quote:Original post by SiCrane
However, it doesn't take a hundred man years to manually write out a move constructor either.

True. In this case it only costs a little time. And the resulting code will still be useful once move constructor/assignment are generated by the compiler.
Quote:Original post by SiCrane
There's no good reason to deliberately write less performant code

Yes there is, compatibility. As soon as you write your own move constructors, C++03 compilers will choke on your code. Of course you can use conditional compilation, but that doesn't make the code any more readable. And once the compilers support the automation, your code is full of irrelevant noise.

What does the latest GCC say to this code?

#include <iostream>struct Foo{    Foo()    {        std::cout << "Foo default\n";    }    Foo(const Foo&)    {        std::cout << "Foo copy\n";    }    Foo(Foo&&)    {        std::cout << "Foo move\n";    }};struct Bar{    Foo foo;};Foo foo(){    return Foo();}Bar bar(){    return Bar();}int main(){    foo();    bar();}
I have 4.4.1 and it says:

Foo defaultFoo default


:)

With -fno-elide-constructors it says:

Foo defaultFoo moveFoo defaultFoo copy
Quote:Original post by visitor
Foo copy

Bummer :(

This topic is closed to new replies.

Advertisement