| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(COMMUNITY Programming in D for C++ Programmers, |
|---|
| 4 |
|
|---|
| 5 |
<img src="cpp1.gif" border=0 align=right alt="C++"> |
|---|
| 6 |
|
|---|
| 7 |
Every experienced C++ programmer accumulates a series of idioms and techniques |
|---|
| 8 |
which become second nature. Sometimes, when learning a new language, those |
|---|
| 9 |
idioms can be so comfortable it's hard to see how to do the equivalent in the |
|---|
| 10 |
new language. So here's a collection of common C++ techniques, and how to do the |
|---|
| 11 |
corresponding task in D. |
|---|
| 12 |
<p> |
|---|
| 13 |
|
|---|
| 14 |
See also: <a href="ctod.html">Programming in D for C Programmers</a> |
|---|
| 15 |
|
|---|
| 16 |
$(UL |
|---|
| 17 |
$(LI $(LINK2 #constructors, Defining Constructors)) |
|---|
| 18 |
$(LI $(LINK2 #baseclass, Base class initialization)) |
|---|
| 19 |
$(LI $(LINK2 #structcmp, Comparing structs)) |
|---|
| 20 |
$(LI $(LINK2 #typedefs, Creating a new typedef'd type)) |
|---|
| 21 |
$(LI $(LINK2 #friends, Friends)) |
|---|
| 22 |
$(LI $(LINK2 #operatoroverloading, Operator overloading)) |
|---|
| 23 |
$(LI $(LINK2 #usingdeclaration, Namespace using declarations)) |
|---|
| 24 |
$(LI $(LINK2 #raii, RAII (Resource Acquisition Is Initialization))) |
|---|
| 25 |
$(LI $(LINK2 #properties, Properties)) |
|---|
| 26 |
$(LI $(LINK2 #recursivetemplates, Recursive Templates)) |
|---|
| 27 |
$(LI $(LINK2 #metatemplates, Meta Templates)) |
|---|
| 28 |
$(LI $(LINK2 #typetraits, Type Traits)) |
|---|
| 29 |
) |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
<hr><!-- -------------------------------------------- --> |
|---|
| 33 |
|
|---|
| 34 |
<h3><a name="constructors">Defining constructors</a></h3> |
|---|
| 35 |
|
|---|
| 36 |
<h4>The C++ Way</h4> |
|---|
| 37 |
|
|---|
| 38 |
Constructors have the same name as the class: |
|---|
| 39 |
|
|---|
| 40 |
$(CPPCODE |
|---|
| 41 |
class Foo |
|---|
| 42 |
{ |
|---|
| 43 |
Foo(int x); |
|---|
| 44 |
}; |
|---|
| 45 |
) |
|---|
| 46 |
|
|---|
| 47 |
<h4>The D Way</h4> |
|---|
| 48 |
|
|---|
| 49 |
Constructors are defined with the this keyword: |
|---|
| 50 |
|
|---|
| 51 |
------ |
|---|
| 52 |
class Foo |
|---|
| 53 |
{ |
|---|
| 54 |
this(int x) { } |
|---|
| 55 |
} |
|---|
| 56 |
------ |
|---|
| 57 |
|
|---|
| 58 |
which reflects how they are used in D. |
|---|
| 59 |
|
|---|
| 60 |
<hr><!-- -------------------------------------------- --> |
|---|
| 61 |
<h3><a name="baseclass">Base class initialization</a></h3> |
|---|
| 62 |
|
|---|
| 63 |
<h4>The C++ Way</h4> |
|---|
| 64 |
|
|---|
| 65 |
Base constructors are called using the base initializer syntax. |
|---|
| 66 |
|
|---|
| 67 |
$(CPPCODE |
|---|
| 68 |
class A { A() {... } }; |
|---|
| 69 |
class B : A |
|---|
| 70 |
{ |
|---|
| 71 |
B(int x) |
|---|
| 72 |
: A() // call base constructor |
|---|
| 73 |
{ ... |
|---|
| 74 |
} |
|---|
| 75 |
};) |
|---|
| 76 |
|
|---|
| 77 |
<h4>The D Way</h4> |
|---|
| 78 |
|
|---|
| 79 |
The base class constructor is called with the super syntax: |
|---|
| 80 |
|
|---|
| 81 |
------ |
|---|
| 82 |
class A { this() { ... } } |
|---|
| 83 |
class B : A |
|---|
| 84 |
{ |
|---|
| 85 |
this(int x) |
|---|
| 86 |
{ ... |
|---|
| 87 |
super(); // call base constructor |
|---|
| 88 |
... |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
------ |
|---|
| 92 |
|
|---|
| 93 |
It's superior to C++ in that the base constructor call can be flexibly placed anywhere in the derived |
|---|
| 94 |
constructor. D can also have one constructor call another one: |
|---|
| 95 |
|
|---|
| 96 |
------ |
|---|
| 97 |
class A |
|---|
| 98 |
{ int a; |
|---|
| 99 |
int b; |
|---|
| 100 |
this() { a = 7; b = foo(); } |
|---|
| 101 |
this(int x) |
|---|
| 102 |
{ |
|---|
| 103 |
this(); |
|---|
| 104 |
a = x; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
------ |
|---|
| 108 |
|
|---|
| 109 |
Members can also be initialized to constants before the constructor is ever called, so the above example is |
|---|
| 110 |
equivalently written as: |
|---|
| 111 |
|
|---|
| 112 |
------ |
|---|
| 113 |
class A |
|---|
| 114 |
{ int a = 7; |
|---|
| 115 |
int b; |
|---|
| 116 |
this() { b = foo(); } |
|---|
| 117 |
this(int x) |
|---|
| 118 |
{ |
|---|
| 119 |
this(); |
|---|
| 120 |
a = x; |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
------ |
|---|
| 124 |
|
|---|
| 125 |
<hr><!-- -------------------------------------------- --> |
|---|
| 126 |
<h3><a name="structcmp">Comparing structs</a></h3> |
|---|
| 127 |
|
|---|
| 128 |
<h4>The C++ Way</h4> |
|---|
| 129 |
|
|---|
| 130 |
While C++ defines struct assignment in a simple, convenient manner: |
|---|
| 131 |
|
|---|
| 132 |
$(CPPCODE |
|---|
| 133 |
struct A x, y; |
|---|
| 134 |
... |
|---|
| 135 |
x = y; |
|---|
| 136 |
) |
|---|
| 137 |
|
|---|
| 138 |
it does not for struct comparisons. Hence, to compare two struct |
|---|
| 139 |
instances for equality: |
|---|
| 140 |
|
|---|
| 141 |
$(CPPCODE |
|---|
| 142 |
#include <string.h> |
|---|
| 143 |
|
|---|
| 144 |
struct A x, y; |
|---|
| 145 |
|
|---|
| 146 |
inline bool operator==(const A& x, const A& y) |
|---|
| 147 |
{ |
|---|
| 148 |
return (memcmp(&x, &y, sizeof(struct A)) == 0); |
|---|
| 149 |
} |
|---|
| 150 |
... |
|---|
| 151 |
if (x == y) |
|---|
| 152 |
... |
|---|
| 153 |
) |
|---|
| 154 |
|
|---|
| 155 |
Note that the operator overload must be done for every struct |
|---|
| 156 |
needing to be compared, and the implementation of that overloaded |
|---|
| 157 |
operator is free of any language help with type checking. |
|---|
| 158 |
The C++ way has an additional problem in that just inspecting the |
|---|
| 159 |
(x == y) does not give a clue what is actually happening, you have |
|---|
| 160 |
to go and find the particular overloaded operator==() that applies |
|---|
| 161 |
to verify what it really does. |
|---|
| 162 |
<p> |
|---|
| 163 |
|
|---|
| 164 |
There's a nasty bug lurking in the memcmp() implementation of operator==(). |
|---|
| 165 |
The layout of a struct, due to alignment, can have $(SINGLEQUOTE holes) in it. |
|---|
| 166 |
C++ does not guarantee those holes are assigned any values, and so |
|---|
| 167 |
two different struct instances can have the same value for each member, |
|---|
| 168 |
but compare different because the holes contain different garbage. |
|---|
| 169 |
<p> |
|---|
| 170 |
|
|---|
| 171 |
To address this, the operator==() can be implemented to do a memberwise |
|---|
| 172 |
compare. Unfortunately, this is unreliable because (1) if a member is added |
|---|
| 173 |
to the struct definition one may forget to add it to operator==(), and |
|---|
| 174 |
(2) floating point nan values compare unequal even if their bit patterns |
|---|
| 175 |
match. |
|---|
| 176 |
<p> |
|---|
| 177 |
|
|---|
| 178 |
There just is no robust solution in C++. |
|---|
| 179 |
|
|---|
| 180 |
<h4>The D Way</h4> |
|---|
| 181 |
|
|---|
| 182 |
D does it the obvious, straightforward way: |
|---|
| 183 |
|
|---|
| 184 |
------ |
|---|
| 185 |
A x, y; |
|---|
| 186 |
... |
|---|
| 187 |
if (x == y) |
|---|
| 188 |
... |
|---|
| 189 |
------ |
|---|
| 190 |
|
|---|
| 191 |
<hr><!-- -------------------------------------------- --> |
|---|
| 192 |
<h3><a name="typedefs">Creating a new typedef'd type</a></h3> |
|---|
| 193 |
|
|---|
| 194 |
<h4>The C++ Way</h4> |
|---|
| 195 |
|
|---|
| 196 |
Typedef's in C++ are weak, that is, they really do not introduce |
|---|
| 197 |
a new type. The compiler doesn't distinguish between a typedef |
|---|
| 198 |
and its underlying type. |
|---|
| 199 |
|
|---|
| 200 |
$(CPPCODE |
|---|
| 201 |
#define HANDLE_INIT ((Handle)(-1)) |
|---|
| 202 |
typedef void *Handle; |
|---|
| 203 |
void foo(void *); |
|---|
| 204 |
void bar(Handle); |
|---|
| 205 |
|
|---|
| 206 |
Handle h = HANDLE_INIT; |
|---|
| 207 |
foo(h); // coding bug not caught |
|---|
| 208 |
bar(h); // ok |
|---|
| 209 |
) |
|---|
| 210 |
|
|---|
| 211 |
The C++ solution is to create a dummy struct whose sole |
|---|
| 212 |
purpose is to get type checking and overloading on the new type. |
|---|
| 213 |
|
|---|
| 214 |
$(CPPCODE |
|---|
| 215 |
#define HANDLE_INIT ((void *)(-1)) |
|---|
| 216 |
struct Handle |
|---|
| 217 |
{ void *ptr; |
|---|
| 218 |
|
|---|
| 219 |
// default initializer |
|---|
| 220 |
Handle() { ptr = HANDLE_INIT; } |
|---|
| 221 |
|
|---|
| 222 |
Handle(int i) { ptr = (void *)i; } |
|---|
| 223 |
|
|---|
| 224 |
// conversion to underlying type |
|---|
| 225 |
operator void*() { return ptr; } |
|---|
| 226 |
}; |
|---|
| 227 |
void bar(Handle); |
|---|
| 228 |
|
|---|
| 229 |
Handle h; |
|---|
| 230 |
bar(h); |
|---|
| 231 |
h = func(); |
|---|
| 232 |
if (h != HANDLE_INIT) |
|---|
| 233 |
... |
|---|
| 234 |
) |
|---|
| 235 |
|
|---|
| 236 |
<h4>The D Way</h4> |
|---|
| 237 |
|
|---|
| 238 |
No need for idiomatic constructions like the above. Just write: |
|---|
| 239 |
|
|---|
| 240 |
------ |
|---|
| 241 |
typedef void* Handle = cast(void*)-1; |
|---|
| 242 |
void bar(Handle); |
|---|
| 243 |
|
|---|
| 244 |
Handle h; |
|---|
| 245 |
bar(h); |
|---|
| 246 |
h = func(); |
|---|
| 247 |
if (h != Handle.init) |
|---|
| 248 |
... |
|---|
| 249 |
------ |
|---|
| 250 |
|
|---|
| 251 |
Note how a default initializer can be supplied for the typedef as |
|---|
| 252 |
a value of the underlying type. |
|---|
| 253 |
|
|---|
| 254 |
<hr><!-- -------------------------------------------- --> |
|---|
| 255 |
<h3><a name="friends">Friends</a></h3> |
|---|
| 256 |
|
|---|
| 257 |
<h4>The C++ Way</h4> |
|---|
| 258 |
|
|---|
| 259 |
Sometimes two classes are tightly related but not by inheritance, |
|---|
| 260 |
but need to access each other's private members. This is done |
|---|
| 261 |
using $(TT friend) declarations: |
|---|
| 262 |
|
|---|
| 263 |
$(CPPCODE |
|---|
| 264 |
class A |
|---|
| 265 |
{ |
|---|
| 266 |
private: |
|---|
| 267 |
int a; |
|---|
| 268 |
|
|---|
| 269 |
public: |
|---|
| 270 |
int foo(B *j); |
|---|
| 271 |
friend class B; |
|---|
| 272 |
friend int abc(A *); |
|---|
| 273 |
}; |
|---|
| 274 |
|
|---|
| 275 |
class B |
|---|
| 276 |
{ |
|---|
| 277 |
private: |
|---|
| 278 |
int b; |
|---|
| 279 |
|
|---|
| 280 |
public: |
|---|
| 281 |
int bar(A *j); |
|---|
| 282 |
friend class A; |
|---|
| 283 |
}; |
|---|
| 284 |
|
|---|
| 285 |
int A::foo(B *j) { return j->b; } |
|---|
| 286 |
int B::bar(A *j) { return j->a; } |
|---|
| 287 |
|
|---|
| 288 |
int abc(A *p) { return p->a; } |
|---|
| 289 |
) |
|---|
| 290 |
|
|---|
| 291 |
<h4>The D Way</h4> |
|---|
| 292 |
|
|---|
| 293 |
In D, friend access is implicit in being a member of the same |
|---|
| 294 |
module. It makes sense that tightly related classes should be |
|---|
| 295 |
in the same module, so implicitly granting friend access to |
|---|
| 296 |
other module members solves the problem neatly: |
|---|
| 297 |
|
|---|
| 298 |
------ |
|---|
| 299 |
module X; |
|---|
| 300 |
|
|---|
| 301 |
class A |
|---|
| 302 |
{ |
|---|
| 303 |
private: |
|---|
| 304 |
static int a; |
|---|
| 305 |
|
|---|
| 306 |
public: |
|---|
| 307 |
int foo(B j) { return j.b; } |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
class B |
|---|
| 311 |
{ |
|---|
| 312 |
private: |
|---|
| 313 |
static int b; |
|---|
| 314 |
|
|---|
| 315 |
public: |
|---|
| 316 |
int bar(A j) { return j.a; } |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
int abc(A p) { return p.a; } |
|---|
| 320 |
------ |
|---|
| 321 |
|
|---|
| 322 |
The $(TT private) attribute prevents other modules from |
|---|
| 323 |
accessing the members. |
|---|
| 324 |
|
|---|
| 325 |
<hr><!-- -------------------------------------------- --> |
|---|
| 326 |
<h3><a name="operatoroverloading">Operator overloading</a></h3> |
|---|
| 327 |
|
|---|
| 328 |
<h4>The C++ Way</h4> |
|---|
| 329 |
|
|---|
| 330 |
Given a struct that creates a new arithmetic data type, |
|---|
| 331 |
it's convenient to overload the comparison operators so |
|---|
| 332 |
it can be compared against integers: |
|---|
| 333 |
|
|---|
| 334 |
$(CPPCODE |
|---|
| 335 |
struct A |
|---|
| 336 |
{ |
|---|
| 337 |
int operator < (int i); |
|---|
| 338 |
int operator <= (int i); |
|---|
| 339 |
int operator > (int i); |
|---|
| 340 |
int operator >= (int i); |
|---|
| 341 |
}; |
|---|
| 342 |
|
|---|
| 343 |
int operator < (int i, A &a) { return a > i; } |
|---|
| 344 |
int operator <= (int i, A &a) { return a >= i; } |
|---|
| 345 |
int operator > (int i, A &a) { return a < i; } |
|---|
| 346 |
int operator >= (int i, A &a) { return a <= i; } |
|---|
| 347 |
) |
|---|
| 348 |
|
|---|
| 349 |
A total of 8 functions are necessary. |
|---|
| 350 |
|
|---|
| 351 |
<h4>The D Way</h4> |
|---|
| 352 |
|
|---|
| 353 |
D recognizes that the comparison operators are all fundamentally |
|---|
| 354 |
related to each other. So only one function is necessary: |
|---|
| 355 |
|
|---|
| 356 |
------ |
|---|
| 357 |
struct A |
|---|
| 358 |
{ |
|---|
| 359 |
int opCmp(int i); |
|---|
| 360 |
} |
|---|
| 361 |
------ |
|---|
| 362 |
|
|---|
| 363 |
The compiler automatically interprets all the |
|---|
| 364 |
<, <=, > and >= |
|---|
| 365 |
operators in terms of the $(TT cmp) function, as well |
|---|
| 366 |
as handling the cases where the left operand is not an |
|---|
| 367 |
object reference. |
|---|
| 368 |
<p> |
|---|
| 369 |
|
|---|
| 370 |
Similar sensible rules hold for other operator overloads, |
|---|
| 371 |
making using operator overloading in D much less tedious and less |
|---|
| 372 |
error prone. Far less code needs to be written to accomplish |
|---|
| 373 |
the same effect. |
|---|
| 374 |
|
|---|
| 375 |
<hr><!-- -------------------------------------------- --> |
|---|
| 376 |
<h3><a name="usingdeclaration">Namespace using declarations</a></h3> |
|---|
| 377 |
|
|---|
| 378 |
<h4>The C++ Way</h4> |
|---|
| 379 |
|
|---|
| 380 |
A $(I using-declaration) in C++ is used to bring a name from |
|---|
| 381 |
a namespace scope into the current scope: |
|---|
| 382 |
|
|---|
| 383 |
$(CPPCODE |
|---|
| 384 |
namespace foo |
|---|
| 385 |
{ |
|---|
| 386 |
int x; |
|---|
| 387 |
} |
|---|
| 388 |
using foo::x; |
|---|
| 389 |
) |
|---|
| 390 |
|
|---|
| 391 |
<h4>The D Way</h4> |
|---|
| 392 |
|
|---|
| 393 |
D uses modules instead of namespaces and #include files, and |
|---|
| 394 |
alias declarations take the place of using declarations: |
|---|
| 395 |
|
|---|
| 396 |
------ |
|---|
| 397 |
/** Module foo.d **/ |
|---|
| 398 |
module foo; |
|---|
| 399 |
int x; |
|---|
| 400 |
|
|---|
| 401 |
/** Another module **/ |
|---|
| 402 |
import foo; |
|---|
| 403 |
alias foo.x x; |
|---|
| 404 |
------ |
|---|
| 405 |
|
|---|
| 406 |
Alias is a much more flexible than the single purpose using |
|---|
| 407 |
declaration. Alias can be used to rename symbols, refer to |
|---|
| 408 |
template members, refer to nested class types, etc. |
|---|
| 409 |
|
|---|
| 410 |
<hr><!-- -------------------------------------------- --> |
|---|
| 411 |
<h3><a name="raii">RAII (Resource Acquisition Is Initialization)</a></h3> |
|---|
| 412 |
|
|---|
| 413 |
<h4>The C++ Way</h4> |
|---|
| 414 |
|
|---|
| 415 |
In C++, resources like memory, etc., all need to be handled |
|---|
| 416 |
explicitly. Since destructors automatically get called when |
|---|
| 417 |
leaving a scope, RAII is implemented by putting the resource |
|---|
| 418 |
release code into the destructor: |
|---|
| 419 |
|
|---|
| 420 |
$(CPPCODE |
|---|
| 421 |
class File |
|---|
| 422 |
{ Handle *h; |
|---|
| 423 |
|
|---|
| 424 |
~File() |
|---|
| 425 |
{ |
|---|
| 426 |
h->release(); |
|---|
| 427 |
} |
|---|
| 428 |
}; |
|---|
| 429 |
) |
|---|
| 430 |
|
|---|
| 431 |
<h4>The D Way</h4> |
|---|
| 432 |
|
|---|
| 433 |
The bulk of resource release problems are simply keeping track |
|---|
| 434 |
of and freeing memory. This is handled automatically in D by |
|---|
| 435 |
the garbage collector. The second common resources used are semaphores |
|---|
| 436 |
and locks, handled automatically with D's $(TT synchronized) |
|---|
| 437 |
declarations and statements. |
|---|
| 438 |
<p> |
|---|
| 439 |
|
|---|
| 440 |
The few RAII issues left are handled by $(I scope) classes. |
|---|
| 441 |
Scope classes get their destructors run when they go out of scope. |
|---|
| 442 |
|
|---|
| 443 |
------ |
|---|
| 444 |
scope class File |
|---|
| 445 |
{ Handle h; |
|---|
| 446 |
|
|---|
| 447 |
~this() |
|---|
| 448 |
{ |
|---|
| 449 |
h.release(); |
|---|
| 450 |
} |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
void test() |
|---|
| 454 |
{ |
|---|
| 455 |
if (...) |
|---|
| 456 |
{ scope f = new File(); |
|---|
| 457 |
... |
|---|
| 458 |
} // f.~this() gets run at closing brace, even if |
|---|
| 459 |
// scope was exited via a thrown exception |
|---|
| 460 |
} |
|---|
| 461 |
------ |
|---|
| 462 |
|
|---|
| 463 |
<hr><!-- -------------------------------------------- --> |
|---|
| 464 |
<h3><a name="properties">Properties</a></h3> |
|---|
| 465 |
|
|---|
| 466 |
<h4>The C++ Way</h4> |
|---|
| 467 |
|
|---|
| 468 |
It is common practice to define a field, |
|---|
| 469 |
along with object-oriented |
|---|
| 470 |
get and set functions for it: |
|---|
| 471 |
|
|---|
| 472 |
$(CPPCODE |
|---|
| 473 |
class Abc |
|---|
| 474 |
{ |
|---|
| 475 |
public: |
|---|
| 476 |
void setProperty(int newproperty) { property = newproperty; } |
|---|
| 477 |
int getProperty() { return property; } |
|---|
| 478 |
|
|---|
| 479 |
private: |
|---|
| 480 |
int property; |
|---|
| 481 |
}; |
|---|
| 482 |
|
|---|
| 483 |
Abc a; |
|---|
| 484 |
a.setProperty(3); |
|---|
| 485 |
int x = a.getProperty(); |
|---|
| 486 |
) |
|---|
| 487 |
|
|---|
| 488 |
All this is quite a bit of typing, and it tends to make |
|---|
| 489 |
code unreadable by filling |
|---|
| 490 |
it with getProperty() and setProperty() calls. |
|---|
| 491 |
|
|---|
| 492 |
<h4>The D Way</h4> |
|---|
| 493 |
|
|---|
| 494 |
Properties can be get and set using the normal field syntax, |
|---|
| 495 |
yet the get and set will invoke methods instead. |
|---|
| 496 |
|
|---|
| 497 |
------ |
|---|
| 498 |
class Abc |
|---|
| 499 |
{ |
|---|
| 500 |
// set |
|---|
| 501 |
void property(int newproperty) { myprop = newproperty; } |
|---|
| 502 |
|
|---|
| 503 |
// get |
|---|
| 504 |
int property() { return myprop; } |
|---|
| 505 |
|
|---|
| 506 |
private: |
|---|
| 507 |
int myprop; |
|---|
| 508 |
} |
|---|
| 509 |
------ |
|---|
| 510 |
|
|---|
| 511 |
which is used as: |
|---|
| 512 |
|
|---|
| 513 |
------ |
|---|
| 514 |
Abc a; |
|---|
| 515 |
a.property = 3; // equivalent to a.property(3) |
|---|
| 516 |
int x = a.property; // equivalent to int x = a.property() |
|---|
| 517 |
------ |
|---|
| 518 |
|
|---|
| 519 |
Thus, in D a property can be treated like it was a simple field name. |
|---|
| 520 |
A property can start out actually being a simple field name, |
|---|
| 521 |
but if later if becomes |
|---|
| 522 |
necessary to make getting and setting it function calls, |
|---|
| 523 |
no code needs to be modified other |
|---|
| 524 |
than the class definition. |
|---|
| 525 |
It obviates the wordy practice of defining get and set properties |
|---|
| 526 |
$(SINGLEQUOTE just in case) a derived class should need to override them. |
|---|
| 527 |
It's also a way to have interface classes, which do not have |
|---|
| 528 |
data fields, behave syntactically as if they did. |
|---|
| 529 |
|
|---|
| 530 |
<hr><!-- -------------------------------------------- --> |
|---|
| 531 |
<h3><a name="recursivetemplates">Recursive Templates</a></h3> |
|---|
| 532 |
|
|---|
| 533 |
<h4>The C++ Way</h4> |
|---|
| 534 |
|
|---|
| 535 |
An advanced use of templates is to recursively expand |
|---|
| 536 |
them, relying on specialization to end it. A template |
|---|
| 537 |
to compute a factorial would be: |
|---|
| 538 |
|
|---|
| 539 |
$(CPPCODE |
|---|
| 540 |
template<int n> class factorial |
|---|
| 541 |
{ |
|---|
| 542 |
public: |
|---|
| 543 |
enum { result = n * factorial<n - 1>::result }; |
|---|
| 544 |
}; |
|---|
| 545 |
|
|---|
| 546 |
template<> class factorial<1> |
|---|
| 547 |
{ |
|---|
| 548 |
public: |
|---|
| 549 |
enum { result = 1 }; |
|---|
| 550 |
}; |
|---|
| 551 |
|
|---|
| 552 |
void test() |
|---|
| 553 |
{ |
|---|
| 554 |
printf("%d\n", factorial<4>::result); // prints 24 |
|---|
| 555 |
} |
|---|
| 556 |
) |
|---|
| 557 |
|
|---|
| 558 |
<h4>The D Way</h4> |
|---|
| 559 |
|
|---|
| 560 |
The D version is analogous, though a little simpler, taking |
|---|
| 561 |
advantage of promotion of single template members to the |
|---|
| 562 |
enclosing name space: |
|---|
| 563 |
|
|---|
| 564 |
------ |
|---|
| 565 |
template factorial(int n) |
|---|
| 566 |
{ |
|---|
| 567 |
enum { factorial = n * .factorial!(n-1) } |
|---|
| 568 |
} |
|---|
| 569 |
|
|---|
| 570 |
template factorial(int n : 1) |
|---|
| 571 |
{ |
|---|
| 572 |
enum { factorial = 1 } |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
void test() |
|---|
| 576 |
{ |
|---|
| 577 |
writefln("%d", factorial!(4)); // prints 24 |
|---|
| 578 |
} |
|---|
| 579 |
------ |
|---|
| 580 |
|
|---|
| 581 |
<hr><!-- -------------------------------------------- --> |
|---|
| 582 |
|
|---|
| 583 |
<h3><a name="metatemplates">Meta Templates</a></h3> |
|---|
| 584 |
|
|---|
| 585 |
The problem: create a typedef for a signed integral type that is at |
|---|
| 586 |
least $(I nbits) in size. |
|---|
| 587 |
|
|---|
| 588 |
<h4>The C++ Way</h4> |
|---|
| 589 |
|
|---|
| 590 |
This example is simplified and adapted from one written by |
|---|
| 591 |
Dr. Carlo Pescio in |
|---|
| 592 |
<a href="http://www.eptacom.net/pubblicazioni/pub_eng/paramint.html"> |
|---|
| 593 |
Template Metaprogramming: Make parameterized integers portable with this novel technique</a>. |
|---|
| 594 |
<p> |
|---|
| 595 |
|
|---|
| 596 |
There is no way in C++ to do conditional compilation based |
|---|
| 597 |
on the result of an expression based on template parameters, so |
|---|
| 598 |
all control flow follows from pattern matching of the template |
|---|
| 599 |
argument against various explicit template specializations. |
|---|
| 600 |
Even worse, there is no way to do template specializations based |
|---|
| 601 |
on relationships like "less than or equal to", so the example |
|---|
| 602 |
uses a clever technique where the template is recursively expanded, |
|---|
| 603 |
incrementing the template value argument by one each time, until |
|---|
| 604 |
a specialization matches. |
|---|
| 605 |
If there is no match, the result is an unhelpful recursive compiler |
|---|
| 606 |
stack overflow or internal error, or at best a strange syntax |
|---|
| 607 |
error. |
|---|
| 608 |
<p> |
|---|
| 609 |
|
|---|
| 610 |
A preprocessor macro is also needed to make up for the lack |
|---|
| 611 |
of template typedefs. |
|---|
| 612 |
|
|---|
| 613 |
$(CPPCODE |
|---|
| 614 |
#include <limits.h> |
|---|
| 615 |
|
|---|
| 616 |
template< int nbits > struct Integer |
|---|
| 617 |
{ |
|---|
| 618 |
typedef Integer< nbits + 1 > :: int_type int_type ; |
|---|
| 619 |
} ; |
|---|
| 620 |
|
|---|
| 621 |
struct Integer< 8 > |
|---|
| 622 |
{ |
|---|
| 623 |
typedef signed char int_type ; |
|---|
| 624 |
} ; |
|---|
| 625 |
|
|---|
| 626 |
struct Integer< 16 > |
|---|
| 627 |
{ |
|---|
| 628 |
typedef short int_type ; |
|---|
| 629 |
} ; |
|---|
| 630 |
|
|---|
| 631 |
struct Integer< 32 > |
|---|
| 632 |
{ |
|---|
| 633 |
typedef int int_type ; |
|---|
| 634 |
} ; |
|---|
| 635 |
|
|---|
| 636 |
struct Integer< 64 > |
|---|
| 637 |
{ |
|---|
| 638 |
typedef long long int_type ; |
|---|
| 639 |
} ; |
|---|
| 640 |
|
|---|
| 641 |
// If the required size is not supported, the metaprogram |
|---|
| 642 |
// will increase the counter until an internal error is |
|---|
| 643 |
// signaled, or INT_MAX is reached. The INT_MAX |
|---|
| 644 |
// specialization does not define a int_type, so a |
|---|
| 645 |
// compiling error is always generated |
|---|
| 646 |
struct Integer< INT_MAX > |
|---|
| 647 |
{ |
|---|
| 648 |
} ; |
|---|
| 649 |
|
|---|
| 650 |
// A bit of syntactic sugar |
|---|
| 651 |
#define Integer( nbits ) Integer< nbits > :: int_type |
|---|
| 652 |
|
|---|
| 653 |
#include <stdio.h> |
|---|
| 654 |
|
|---|
| 655 |
int main() |
|---|
| 656 |
{ |
|---|
| 657 |
Integer( 8 ) i ; |
|---|
| 658 |
Integer( 16 ) j ; |
|---|
| 659 |
Integer( 29 ) k ; |
|---|
| 660 |
Integer( 64 ) l ; |
|---|
| 661 |
printf("%d %d %d %d\n", |
|---|
| 662 |
sizeof(i), sizeof(j), sizeof(k), sizeof(l)); |
|---|
| 663 |
return 0 ; |
|---|
| 664 |
} |
|---|
| 665 |
) |
|---|
| 666 |
|
|---|
| 667 |
<h4>The C++ Boost Way</h4> |
|---|
| 668 |
|
|---|
| 669 |
This version uses the C++ Boost library. It was provided |
|---|
| 670 |
by David Abrahams. |
|---|
| 671 |
|
|---|
| 672 |
$(CPPCODE |
|---|
| 673 |
#include <boost/mpl/if.hpp> |
|---|
| 674 |
#include <boost/mpl/assert.hpp> |
|---|
| 675 |
|
|---|
| 676 |
template <int nbits> struct Integer |
|---|
| 677 |
: mpl::if_c<(nbits <= 8), signed char |
|---|
| 678 |
, mpl::if_c<(nbits <= 16), short |
|---|
| 679 |
, mpl::if_c<(nbits <= 32), long |
|---|
| 680 |
, long long>::type >::type > |
|---|
| 681 |
{ |
|---|
| 682 |
BOOST_MPL_ASSERT_RELATION(nbits, <=, 64); |
|---|
| 683 |
} |
|---|
| 684 |
|
|---|
| 685 |
#include <stdio.h> |
|---|
| 686 |
|
|---|
| 687 |
int main() |
|---|
| 688 |
{ |
|---|
| 689 |
Integer< 8 > i ; |
|---|
| 690 |
Integer< 16 > j ; |
|---|
| 691 |
Integer< 29 > k ; |
|---|
| 692 |
Integer< 64 > l ; |
|---|
| 693 |
printf("%d %d %d %d\n", |
|---|
| 694 |
sizeof(i), sizeof(j), sizeof(k), sizeof(l)); |
|---|
| 695 |
return 0 ; |
|---|
| 696 |
} |
|---|
| 697 |
) |
|---|
| 698 |
|
|---|
| 699 |
<h4>The D Way</h4> |
|---|
| 700 |
|
|---|
| 701 |
The D version could also be written with recursive templates, |
|---|
| 702 |
but there's a better way. |
|---|
| 703 |
Unlike the C++ example, this one is fairly easy to |
|---|
| 704 |
figure out what is going on. |
|---|
| 705 |
It compiles quickly, and gives a sensible compile time message |
|---|
| 706 |
if it fails. |
|---|
| 707 |
|
|---|
| 708 |
------ |
|---|
| 709 |
import std.stdio; |
|---|
| 710 |
|
|---|
| 711 |
template Integer(int nbits) |
|---|
| 712 |
{ |
|---|
| 713 |
static if (nbits <= 8) |
|---|
| 714 |
alias byte Integer; |
|---|
| 715 |
else static if (nbits <= 16) |
|---|
| 716 |
alias short Integer; |
|---|
| 717 |
else static if (nbits <= 32) |
|---|
| 718 |
alias int Integer; |
|---|
| 719 |
else static if (nbits <= 64) |
|---|
| 720 |
alias long Integer; |
|---|
| 721 |
else |
|---|
| 722 |
static assert(0); |
|---|
| 723 |
} |
|---|
| 724 |
|
|---|
| 725 |
int main() |
|---|
| 726 |
{ |
|---|
| 727 |
Integer!(8) i ; |
|---|
| 728 |
Integer!(16) j ; |
|---|
| 729 |
Integer!(29) k ; |
|---|
| 730 |
Integer!(64) l ; |
|---|
| 731 |
writefln("%d %d %d %d", |
|---|
| 732 |
i.sizeof, j.sizeof, k.sizeof, l.sizeof); |
|---|
| 733 |
return 0; |
|---|
| 734 |
} |
|---|
| 735 |
------ |
|---|
| 736 |
|
|---|
| 737 |
<hr><!-- -------------------------------------------- --> |
|---|
| 738 |
|
|---|
| 739 |
<h3><a name="typetraits">Type Traits</a></h3> |
|---|
| 740 |
|
|---|
| 741 |
Type traits are another term for being able to find out |
|---|
| 742 |
properties of a type at compile time. |
|---|
| 743 |
|
|---|
| 744 |
<h4>The C++ Way</h4> |
|---|
| 745 |
|
|---|
| 746 |
The following template comes from |
|---|
| 747 |
<a href="http://www.amazon.com/exec/obidos/ASIN/0201734842/ref=ase_classicempire/102-2957199-2585768"> |
|---|
| 748 |
C++ Templates: The Complete Guide, David Vandevoorde, Nicolai M. Josuttis</a> |
|---|
| 749 |
pg. 353 which determines if the template's argument type |
|---|
| 750 |
is a function: |
|---|
| 751 |
|
|---|
| 752 |
$(CPPCODE |
|---|
| 753 |
template<typename T> class IsFunctionT |
|---|
| 754 |
{ |
|---|
| 755 |
private: |
|---|
| 756 |
typedef char One; |
|---|
| 757 |
typedef struct { char a[2]; } Two; |
|---|
| 758 |
template<typename U> static One test(...); |
|---|
| 759 |
template<typename U> static Two test(U (*)[1]); |
|---|
| 760 |
public: |
|---|
| 761 |
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 }; |
|---|
| 762 |
}; |
|---|
| 763 |
|
|---|
| 764 |
void test() |
|---|
| 765 |
{ |
|---|
| 766 |
typedef int (fp)(int); |
|---|
| 767 |
|
|---|
| 768 |
assert(IsFunctionT<fp>::Yes == 1); |
|---|
| 769 |
} |
|---|
| 770 |
) |
|---|
| 771 |
|
|---|
| 772 |
This template relies on the $(SFINAE) principle. |
|---|
| 773 |
Why it works is a fairly advanced template topic. |
|---|
| 774 |
|
|---|
| 775 |
<h4>The D Way</h4> |
|---|
| 776 |
|
|---|
| 777 |
$(ACRONYM SFINAE, Substitution Failure Is Not An Error) |
|---|
| 778 |
can be done in D without resorting to template argument |
|---|
| 779 |
pattern matching: |
|---|
| 780 |
|
|---|
| 781 |
------ |
|---|
| 782 |
template IsFunctionT(T) |
|---|
| 783 |
{ |
|---|
| 784 |
static if ( is(T[]) ) |
|---|
| 785 |
const int IsFunctionT = 0; |
|---|
| 786 |
else |
|---|
| 787 |
const int IsFunctionT = 1; |
|---|
| 788 |
} |
|---|
| 789 |
|
|---|
| 790 |
void test() |
|---|
| 791 |
{ |
|---|
| 792 |
typedef int fp(int); |
|---|
| 793 |
|
|---|
| 794 |
assert(IsFunctionT!(fp) == 1); |
|---|
| 795 |
} |
|---|
| 796 |
------ |
|---|
| 797 |
|
|---|
| 798 |
The task of discovering if a type is a function doesn't need a |
|---|
| 799 |
template at all, nor does it need the subterfuge of attempting to |
|---|
| 800 |
create the invalid array of functions type. |
|---|
| 801 |
The $(ISEXPRESSION) expression can test it directly: |
|---|
| 802 |
|
|---|
| 803 |
------ |
|---|
| 804 |
void test() |
|---|
| 805 |
{ |
|---|
| 806 |
alias int fp(int); |
|---|
| 807 |
|
|---|
| 808 |
assert( is(fp == function) ); |
|---|
| 809 |
} |
|---|
| 810 |
------ |
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
) |
|---|
| 814 |
|
|---|
| 815 |
Macros: |
|---|
| 816 |
TITLE=Programming in D for C++ Programmers |
|---|
| 817 |
WIKI=CPPtoD |
|---|