Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "ParamPassTest.h"
#include <exception>
#include <utility>
#include <memory>
#include <type_traits>
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ParamPassTest, "bits" );
CPPUNIT_TEST_SUITE_REGISTRATION ( ParamPassTest );
void ParamPassTest::setUp ( ) {
}
void ParamPassTest::tearDown ( ) {
}
int instances = 0;
class FooCloneable {
int data;
FooCloneable ( const FooCloneable & foo ) : data ( foo.data ) {
std::cout << "Copy" << std::endl;
instances++;
}
FooCloneable ( FooCloneable && foo ) : data ( foo.data ) {
std::cout << "Move" << std::endl;
instances++;
}
public:
FooCloneable ( int data ) : data ( data ) {
std::cout << "Constructor" << std::endl;
instances++;
}
~FooCloneable ( ) {
std::cout << "Destructor" << std::endl;
instances--;
}
FooCloneable * clone ( ) const {
return new FooCloneable ( * this );
}
int getData ( ) const {
return data;
}
};
class FooCopyable {
int data;
public:
FooCopyable ( int data ) : data ( data ) {
std::cout << "Constructor" << std::endl;
instances++;
}
FooCopyable ( const FooCopyable & foo ) : data ( foo.data ) {
std::cout << "Copy" << std::endl;
instances++;
}
FooCopyable ( FooCopyable && foo ) : data ( foo.data ) {
std::cout << "Move" << std::endl;
instances++;
}
~FooCopyable ( ) {
std::cout << "Destructor" << std::endl;
instances--;
}
FooCopyable * clone ( ) const {
return new FooCopyable ( * this );
}
int getData ( ) const {
return data;
}
};
int dest ( FooCloneable && foo ) {
std::cout << "Destination called" << std::endl;
if ( instances < 2 )
throw std::exception ( );
return foo.getData ( );
}
int dest ( FooCopyable && foo ) {
std::cout << "Destination called" << std::endl;
if ( instances < 2 )
throw std::exception ( );
return foo.getData ( );
}
int test ( FooCloneable && foo ) {
return dest ( std::move ( foo ) );
}
int test ( const FooCloneable & foo ) {
}
int test ( FooCopyable && foo ) {
return dest ( std::move ( foo ) );
}
int test ( const FooCopyable & foo ) {
}
void ParamPassTest::testParameterPassing ( ) {
{
const FooCloneable foo ( 1 );
int res = 0;
CPPUNIT_ASSERT_NO_THROW ( res = test ( foo ) );
CPPUNIT_ASSERT ( res == 1 && instances == 1 );
}
{
const FooCopyable foo ( 1 );
int res = 0;
CPPUNIT_ASSERT_NO_THROW ( res = test ( foo ) );
CPPUNIT_ASSERT ( res == 1 && instances == 1 );
}
}