| 1 |
// EXTRA_CPP_SOURCES: cpp_nonpod_byval.cpp |
| 2 |
// CXXFLAGS(linux osx freebsd dragonflybsd): -std=c++11 |
| 3 |
|
| 4 |
extern (C) int printf(const(char)*, ...); |
| 5 |
|
| 6 |
extern (C++): |
| 7 |
|
| 8 |
template Foo(T) |
| 9 |
{ |
| 10 |
T fooCpp(T param); // calls fooD() with a new, doubled literal |
| 11 |
|
| 12 |
T fooD(T param) |
| 13 |
{ |
| 14 |
printf("fooD %d [%p]\n", param.a, ¶m); |
| 15 |
assert(param.a == 2 * 123); |
| 16 |
static if (__traits(compiles, { T copy = param; })) |
| 17 |
return param; // invokes postblit |
| 18 |
else |
| 19 |
return T(param.a); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
void test(T)() |
| 24 |
{ |
| 25 |
printf(".: %.*s\n", cast(int) T.stringof.length, T.stringof.ptr); |
| 26 |
|
| 27 |
{ |
| 28 |
auto result = fooCpp(T(123)); |
| 29 |
assert(result.a == 246); |
| 30 |
} |
| 31 |
|
| 32 |
static if (__traits(hasMember, T, "numDtor")) |
| 33 |
{ |
| 34 |
// fooCpp param + fooD param + result => 3 T instances. |
| 35 |
// There may be an additional destruction of the moved-from T literal |
| 36 |
// in fooCpp, depending on in-place construction vs. move. |
| 37 |
assert(T.numDtor == 3 || T.numDtor == 4); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
struct POD |
| 42 |
{ |
| 43 |
int a; |
| 44 |
} |
| 45 |
mixin Foo!POD; |
| 46 |
|
| 47 |
struct CtorOnly |
| 48 |
{ |
| 49 |
int a; |
| 50 |
this(int a) { this.a = a; } |
| 51 |
} |
| 52 |
mixin Foo!CtorOnly; |
| 53 |
|
| 54 |
struct DtorOnly |
| 55 |
{ |
| 56 |
static __gshared int numDtor = 0; |
| 57 |
int a; |
| 58 |
~this() { printf("dtor %d [%p]\n", a, &this); ++numDtor; } |
| 59 |
} |
| 60 |
mixin Foo!DtorOnly; |
| 61 |
|
| 62 |
struct CtorDtor |
| 63 |
{ |
| 64 |
static __gshared int numDtor = 0; |
| 65 |
int a; |
| 66 |
this(int a) { this.a = a; } |
| 67 |
~this() { printf("dtor %d [%p]\n", a, &this); ++numDtor; } |
| 68 |
} |
| 69 |
mixin Foo!CtorDtor; |
| 70 |
|
| 71 |
struct Copy |
| 72 |
{ |
| 73 |
static __gshared int numDtor = 0; |
| 74 |
int a; |
| 75 |
this(int a) { this.a = a; } |
| 76 |
~this() { printf("dtor %d [%p]\n", a, &this); ++numDtor; } |
| 77 |
this(this) { printf("post %d [%p]\n", a, &this); } |
| 78 |
} |
| 79 |
mixin Foo!Copy; |
| 80 |
|
| 81 |
struct CopyAndMove |
| 82 |
{ |
| 83 |
static __gshared int numDtor = 0; |
| 84 |
int a; |
| 85 |
this(int a) { this.a = a; } |
| 86 |
~this() { printf("dtor %d [%p]\n", a, &this); ++numDtor; } |
| 87 |
this(this) { printf("post %d [%p]\n", a, &this); } |
| 88 |
} |
| 89 |
mixin Foo!CopyAndMove; |
| 90 |
|
| 91 |
struct MoveOnly |
| 92 |
{ |
| 93 |
static __gshared int numDtor = 0; |
| 94 |
int a; |
| 95 |
this(int a) { this.a = a; } |
| 96 |
~this() { printf("dtor %d [%p]\n", a, &this); ++numDtor; } |
| 97 |
this(this) @disable; |
| 98 |
} |
| 99 |
mixin Foo!MoveOnly; |
| 100 |
|
| 101 |
struct MemberWithCtor |
| 102 |
{ |
| 103 |
int a; |
| 104 |
CtorOnly m; |
| 105 |
} |
| 106 |
mixin Foo!MemberWithCtor; |
| 107 |
|
| 108 |
void main() |
| 109 |
{ |
| 110 |
test!POD(); |
| 111 |
test!CtorOnly(); |
| 112 |
test!DtorOnly(); |
| 113 |
test!CtorDtor(); |
| 114 |
test!Copy(); |
| 115 |
test!CopyAndMove(); |
| 116 |
test!MoveOnly(); |
| 117 |
test!MemberWithCtor(); |
| 118 |
} |