Skip to content

Commit 82f057b

Browse files
authored
Merge pull request #1552 from ntrel/ctod-docs
Tweak D for C, C++ programmer pages
2 parents b7a4561 + 551af09 commit 82f057b

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

cpptod.dd

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Foo
4545

4646
$(H4 The D Way)
4747

48-
Constructors are defined with the this keyword:
48+
Constructors are defined with the `this` keyword:
4949

5050
------
5151
class Foo
@@ -76,7 +76,7 @@ class B : A
7676

7777
$(H4 The D Way)
7878

79-
The base class constructor is called with the super syntax:
79+
The base class constructor is called with the `super` syntax:
8080

8181
------
8282
class A { this() { ... } }
@@ -573,24 +573,33 @@ void test()
573573
$(H4 The D Way)
574574

575575
The D version is analogous, though a little simpler, taking
576-
advantage of promotion of single template members to the
576+
advantage of
577+
$(HTTPS dlang.org/spec/template.html#implicit_template_properties,
578+
Eponymous Templates) - promotion of single template members to the
577579
enclosing name space:
578-
579580
------
580581
template factorial(int n)
581582
{
582-
enum { factorial = n * .factorial!(n-1) }
583+
enum factorial = n * .factorial!(n-1);
583584
}
584585

585586
template factorial(int n : 1)
586587
{
587-
enum { factorial = 1 }
588+
enum factorial = 1;
588589
}
589590

590591
void test()
591592
{
592-
writefln("%d", factorial!(4)); // prints 24
593+
writeln(factorial!(4)); // prints 24
593594
}
595+
------
596+
The template blocks can be made shorter using
597+
$(HTTPS dlang.org/spec/template.html#variable-template,
598+
Enum Template) syntax:
599+
------
600+
enum factorial(int n) = n * .factorial!(n-1);
601+
602+
enum factorial(int n : 1) = 1;
594603
------
595604

596605
<hr>$(COMMENT -------------------------------------------- )
@@ -800,7 +809,7 @@ template IsFunctionT(T)
800809

801810
void test()
802811
{
803-
typedef int fp(int);
812+
alias int fp(int);
804813

805814
assert(IsFunctionT!(fp) == 1);
806815
}

ctod.dd

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ sizeof(struct Foo)
7878

7979
$(H4 The D Way)
8080

81-
$(P Use the size property:)
81+
$(P Use the `sizeof` property:)
8282

8383
----------------------------
8484
int.sizeof
@@ -137,8 +137,8 @@ _Imaginary long double =&gt; ireal
137137
_Complex long double =&gt; creal
138138
)
139139
$(P
140-
Although char is an unsigned 8 bit type, and
141-
wchar is an unsigned 16 bit type, they have their own separate types
140+
Although `char` is an unsigned 8-bit type, and
141+
`wchar` is an unsigned 16-bit type, they have their own separate types
142142
in order to aid overloading and type safety.
143143
)
144144
$(P Ints and unsigneds in C are of varying size; not so in D.)
@@ -194,7 +194,7 @@ long double r = fmodl(x,y);
194194

195195
$(H4 The D Way)
196196

197-
D supports the remainder ('%') operator on floating point operands:
197+
D supports the remainder (`%`) operator on floating point operands:
198198

199199
----------------------------
200200
float f = x % y;
@@ -295,7 +295,7 @@ for (i = 0; i &lt; sizeof(array) / sizeof(array[0]); i++)
295295

296296
$(H4 The D Way)
297297

298-
The length of an array is accessible through the property "length".
298+
The length of an array is accessible through the `length` property.
299299

300300
----------------------------
301301
int[17] array;
@@ -319,7 +319,7 @@ foreach (value; array)
319319
func(value);
320320
----------------------------
321321

322-
or through the ref keyword (for reference access):
322+
or through the `ref` keyword (for reference access):
323323

324324
----------------------------
325325
int[17] array;
@@ -406,7 +406,7 @@ memcpy(s + lens, hello, sizeof(hello));
406406

407407
$(H4 The D Way)
408408

409-
D overloads the operators ~ and ~= for char and wchar arrays to mean
409+
D overloads the operators `~` and `~=` for char and wchar arrays to mean
410410
concatenate and append, respectively:
411411

412412
----------------------------
@@ -582,7 +582,7 @@ $(H3 <a name="tagspace">Struct tag name space</a>)
582582

583583
$(H4 The C Way)
584584

585-
It's annoying to have to put the struct keyword every time a type is specified,
585+
It's annoying to have to use the `struct` keyword every time a type is specified,
586586
so a common idiom is to use:
587587

588588
$(CCODE
@@ -1181,7 +1181,7 @@ if (strcmp(str, "betty") == 0) // do strings match?
11811181

11821182
$(H4 The D Way)
11831183

1184-
Why not use the == operator?
1184+
Why not use the `==` operator?
11851185

11861186
-----------------------------
11871187
string str = "hello";
@@ -1394,7 +1394,7 @@ $(H3 <a name="ushr">Unsigned Right Shift</a>)
13941394

13951395
$(H4 The C Way)
13961396

1397-
The right shift operators &gt;&gt; and &gt;&gt;= are signed
1397+
The right shift operators `>>` and `>>=` are signed
13981398
shifts if the left operand is a signed integral type, and
13991399
are unsigned right shifts if the left operand is an unsigned
14001400
integral type. To produce an unsigned right shift on an int,
@@ -1422,9 +1422,9 @@ j = (unsigned)i >> 3;
14221422

14231423
$(H4 The D Way)
14241424

1425-
D has the right shift operators &gt;&gt; and &gt;&gt;= which
1425+
D has the right shift operators `>>` and `>>=` which
14261426
behave as they do in C. But D also has explicitly unsigned
1427-
right shift operators &gt;&gt;&gt; and &gt;&gt;&gt;= which will
1427+
right shift operators `>>>` and `>>>=` which will
14281428
do an unsigned right shift regardless of the sign of the left
14291429
operand. Hence,
14301430

@@ -1579,7 +1579,7 @@ int main()
15791579

15801580
$(H4 The D Way)
15811581

1582-
The ... following an array parameter declaration means that
1582+
The `...` following an array parameter declaration means that
15831583
the trailing arguments are collected together to form
15841584
an array. The arguments are type checked against the array
15851585
type, and the number of arguments becomes a property

0 commit comments

Comments
 (0)