1 module it.cpp.function_; 2 3 import it; 4 5 @("ref basic param") 6 unittest { 7 shouldCompile( 8 Cpp( 9 q{ 10 void fun(int& i); 11 void gun(double& i); 12 } 13 ), 14 D( 15 q{ 16 int i; 17 fun(i); 18 static assert(!__traits(compiles, fun(4))); 19 20 double d; 21 gun(d); 22 static assert(!__traits(compiles, gun(33.3))); 23 24 } 25 ), 26 ); 27 } 28 29 @("ref struct param") 30 unittest { 31 shouldCompile( 32 Cpp( 33 q{ 34 struct Foo { int i; double d; }; 35 void fun(Foo& f); 36 } 37 ), 38 D( 39 q{ 40 auto f = Foo(2, 33.3); 41 fun(f); 42 static assert(!__traits(compiles, fun(Foo(2, 33.3)))); 43 } 44 ), 45 ); 46 } 47 48 49 @("ref basic return") 50 unittest { 51 shouldCompile( 52 Cpp( 53 q{ 54 int& fun(); 55 double& gun(); 56 } 57 ), 58 D( 59 q{ 60 auto i = fun(); 61 static assert(is(typeof(i) == int), typeof(i).stringof); 62 63 auto d = gun(); 64 static assert(is(typeof(d) == double), typeof(i).stringof); 65 } 66 ), 67 ); 68 } 69 70 @("ref struct return") 71 unittest { 72 shouldCompile( 73 Cpp( 74 q{ 75 struct Foo { int i; }; 76 Foo& fun(); 77 } 78 ), 79 D( 80 q{ 81 auto f = fun(); 82 static assert(is(typeof(f) == Foo), typeof(i).stringof); 83 } 84 ), 85 ); 86 } 87 88 89 @("parameter.std.string") 90 @safe unittest { 91 shouldCompile( 92 Cpp( 93 q{ 94 namespace std { 95 template <typename CharT> 96 struct basic_string {}; 97 98 // naming it string clashes with D's immutable(char)[] alias 99 using mystring = basic_string<char>; 100 } 101 102 struct Foo { 103 // the parameter used to get translated as 104 // basic_string without a template parameter 105 void fun(const std::mystring&); 106 }; 107 } 108 ), 109 D( 110 q{ 111 auto foo = Foo(); 112 } 113 ), 114 ); 115 } 116 117 118 @ShouldFail("Cannot currently handle templated opBinary. See dpp.translation.function_.functionDecl FIXME") 119 @("opBinary") 120 @safe unittest { 121 shouldCompile( 122 Cpp( 123 q{ 124 struct Foo { 125 template<typename T> 126 int operator+(const T& other); 127 }; 128 } 129 ), 130 D( 131 q{ 132 auto foo = Foo(); 133 int ret = foo + 42; 134 } 135 ), 136 ); 137 } 138 139 140 @("opIndex") 141 @safe unittest { 142 shouldCompile( 143 Cpp( 144 q{ 145 struct Foo { 146 int operator[](int i); 147 }; 148 } 149 ), 150 D( 151 q{ 152 auto foo = Foo(); 153 int ret = foo[42]; 154 } 155 ), 156 ); 157 } 158 159 160 @("opOpAssign") 161 @safe unittest { 162 shouldCompile( 163 Cpp( 164 q{ 165 struct Foo { 166 void operator+=(int i); 167 }; 168 } 169 ), 170 D( 171 q{ 172 auto foo = Foo(); 173 foo += 42; 174 } 175 ), 176 ); 177 } 178 179 180 @("opBang") 181 @safe unittest { 182 shouldCompile( 183 Cpp( 184 q{ 185 struct Foo { 186 bool operator!() const; 187 }; 188 } 189 ), 190 D( 191 q{ 192 auto foo = Foo(); 193 bool maybe = !foo; 194 } 195 ), 196 ); 197 }