mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-16 07:48:44 +01:00
Final tidbits before completion of 9.0.0
git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@572 f6dd340e-d3f9-0310-b409-bdd246841980
This commit is contained in:
parent
3e26e7a7dc
commit
26fba70cb5
6 changed files with 56 additions and 8 deletions
|
@ -35,9 +35,9 @@ class Class
|
|||
{
|
||||
public:
|
||||
Class() = default;
|
||||
|
||||
Class(int) {}
|
||||
|
||||
Class(Class const &other) = default;
|
||||
Class(int)
|
||||
{}
|
||||
Class(Class &&tmp)
|
||||
{
|
||||
cout << "Move constructor\n";
|
||||
|
@ -57,8 +57,8 @@ int main()
|
|||
{
|
||||
Class lhs;
|
||||
Class rhs;
|
||||
|
||||
Class result;
|
||||
|
||||
result = lhs + rhs;
|
||||
result = factory() + rhs;
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ includefile(memory/this)
|
|||
includefile(memory/sequential)
|
||||
|
||||
lsect(COPYCONS)(The copy constructor: initialization vs. assignment)
|
||||
includefile(memory/initialisation)
|
||||
includefile(memory/initialization)
|
||||
|
||||
lsect(CopyDestroy)(Revising the assignment operator)
|
||||
includefile(memory/revising)
|
||||
|
@ -61,7 +61,7 @@ includefile(memory/revising)
|
|||
lsect(MOVE)(Moving data (C++0x))
|
||||
includefile(memory/moving.yo)
|
||||
|
||||
subsect(The move constructor (dynamic data) (C++0x))
|
||||
lsubsect(MOVECONS)(The move constructor (dynamic data) (C++0x))
|
||||
includefile(memory/move)
|
||||
|
||||
subsect(The move constructor (composition) (C++0x))
|
||||
|
|
14
yo/memory/examples/copycons.cc
Normal file
14
yo/memory/examples/copycons.cc
Normal file
|
@ -0,0 +1,14 @@
|
|||
class Class
|
||||
{
|
||||
public:
|
||||
Class() = default;
|
||||
Class(Class const &other) = default;
|
||||
Class(Class &&tmp)
|
||||
{}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Class one;
|
||||
Class two(one);
|
||||
}
|
|
@ -46,9 +46,11 @@ argument list.
|
|||
compilation error even though it hasn't been declared in the class
|
||||
interface. This takes us to the following rule:
|
||||
quote(
|
||||
A copy constructor is always available, even if it isn't declared in
|
||||
the class's interface.
|
||||
A copy constructor is (almost) always available, even if it isn't declared
|
||||
in the class's interface.
|
||||
)
|
||||
The reason for the `(almost)' will be given in section ref(MOVECONS).
|
||||
|
||||
The copy constructor made available by the compiler is also called the
|
||||
i(trivial copy constructor). Starting with the C++0x standard it can easily be
|
||||
suppressed (using the tt(= delete) idiom). The trivial copy constructor
|
|
@ -42,3 +42,22 @@ cannot be accessed by other code) and the temporary objects will cease to
|
|||
exist shortly after the constructor's call. Here is the implementation of
|
||||
tt(Strings) move constructor:
|
||||
verbinclude(examples/stringsmove.cc)
|
||||
|
||||
In section ref(COPYCONS) it was stated that the copy constructor is almost
|
||||
always available. em(Almost) always as the declaration of a move constructor
|
||||
suppresses the default availability of the copy constructor.
|
||||
|
||||
The following example shows a simple class tt(Class), declaring a move
|
||||
constructor. In the tt(main) function following the class interface a
|
||||
tt(Class) object is defined which is then passed to the constructor of a
|
||||
second tt(Class) object. Compilation fails with the compiler reporting:
|
||||
verb(
|
||||
error: cannot bind 'Class' lvalue to 'Class&&'
|
||||
error: initializing argument 1 of 'Class::Class(Class&&)'
|
||||
)
|
||||
|
||||
verbinclude(nocopycons.cc)
|
||||
|
||||
The cure is easy: after declaring a (possibly tt(default)) copy
|
||||
constructor the error disappears:
|
||||
verbinclude(examples/copycons.cc)
|
||||
|
|
13
yo/memory/nocopycons.cc
Normal file
13
yo/memory/nocopycons.cc
Normal file
|
@ -0,0 +1,13 @@
|
|||
class Class
|
||||
{
|
||||
public:
|
||||
Class() = default;
|
||||
Class(Class &&tmp)
|
||||
{}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Class one;
|
||||
Class two(one);
|
||||
}
|
Loading…
Reference in a new issue