@@ -213,6 +213,7 @@ $(GNAME AlignAttribute):
213
213
sets it to the default, which matches the default member alignment
214
214
of the companion C compiler.)
215
215
216
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
216
217
--------
217
218
struct S
218
219
{
@@ -221,8 +222,9 @@ struct S
221
222
int b; // placed at offset 4
222
223
long c; // placed at offset 8
223
224
}
224
- auto sz = S.sizeof; // 16
225
+ static assert( S.sizeof == 16);
225
226
--------
227
+ )
226
228
227
229
$(P $(I AssignExpression) specifies the alignment
228
230
which matches the behavior of the companion C compiler when non-default
@@ -233,6 +235,7 @@ auto sz = S.sizeof; // 16
233
235
fields are packed together.
234
236
)
235
237
238
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
236
239
--------
237
240
struct S
238
241
{
@@ -241,8 +244,9 @@ struct S
241
244
int b; // placed at offset 1
242
245
long c; // placed at offset 5
243
246
}
244
- auto sz = S.sizeof; // 16
247
+ static assert( S.sizeof == 13);
245
248
--------
249
+ )
246
250
247
251
$(P The alignment for the fields of an aggregate does not affect the alignment
248
252
of the aggregate itself - that is affected by the alignment setting outside
@@ -256,7 +260,7 @@ align (2) struct S
256
260
int b; // placed at offset 1
257
261
long c; // placed at offset 5
258
262
}
259
- auto sz = S.sizeof; // 14
263
+ static assert( S.sizeof == 14);
260
264
--------
261
265
262
266
$(P Setting the alignment of a field aligns it to that power of 2, regardless
@@ -270,7 +274,7 @@ struct S
270
274
byte b; // placed at offset 4
271
275
short c; // placed at offset 8
272
276
}
273
- auto sz = S.sizeof; // 12
277
+ static assert( S.sizeof == 12);
274
278
--------
275
279
276
280
@@ -307,14 +311,19 @@ $(GNAME DeprecatedAttribute):
307
311
if any code refers to deprecated declarations:
308
312
)
309
313
314
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
310
315
---------------
311
- deprecated
316
+ struct Foo
312
317
{
313
- void oldFoo();
318
+ deprecated
319
+ {
320
+ void oldFoo();
321
+ }
314
322
}
315
323
316
- oldFoo(); // Deprecated: function test.oldFoo is deprecated
324
+ Foo(). oldFoo(); // Deprecated: function test.oldFoo is deprecated
317
325
---------------
326
+ )
318
327
319
328
$(P Optionally a string literal or manifest constant can be used
320
329
to provide additional information in the deprecation message.
@@ -328,16 +337,23 @@ $(GNAME DeprecatedAttribute):
328
337
$(P Calling CTFE-able functions or using manifest constants is also possible.
329
338
)
330
339
340
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
331
341
---------------
332
342
import std.format;
333
343
enum Message = format("%s and all its members are obsolete", Foobar.stringof);
334
344
deprecated(Message) class Foobar {}
335
- auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
336
- // and all its members are obsolete
345
+
337
346
deprecated(format("%s is also obsolete", "This class")) class BarFoo {}
338
- auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
339
- // class is also obsolete
347
+
348
+ void main()
349
+ {
350
+ auto f = new Foobar(); // Deprecated: class test.Foobar is deprecated - Foobar
351
+ // and all its members are obsolete
352
+ auto bf = new BarFoo(); // Deprecated: class test.BarFoo is deprecated - This
353
+ // class is also obsolete
354
+ }
340
355
---------------
356
+ )
341
357
342
358
$(P $(D Implementation Note:) The compiler should have a switch
343
359
specifying if $(D deprecated) should be ignored, cause a warning, or cause an error during compilation.
@@ -464,6 +480,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
464
480
except that the variable is shared by all threads rather than being
465
481
thread local.)
466
482
483
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
467
484
---
468
485
class Foo
469
486
{
@@ -476,6 +493,7 @@ $(H2 $(LNAME2 gshared, $(D __gshared) Attribute))
476
493
return bar++; // Not thread safe.
477
494
}
478
495
---
496
+ )
479
497
480
498
$(P Unlike the $(D shared) attribute, $(D __gshared) provides no
481
499
safe-guards against data races or other multi-threaded synchronization
@@ -491,12 +509,13 @@ causes a compile time error. This can be used to explicitly disallow certain
491
509
operations or overloads at compile time rather than relying on generating a
492
510
runtime error.)
493
511
512
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
494
513
---
495
514
@disable void foo() { }
496
- ---
497
- ---
515
+
498
516
void main() { foo(); /* error, foo is disabled */ }
499
517
---
518
+ )
500
519
501
520
$(P $(DDSUBLINK spec/struct, Struct-Constructor, Disabling struct no-arg constructor)
502
521
disallows default construction of the struct.
@@ -544,6 +563,7 @@ $(H2 $(LNAME2 override, $(D override) Attribute))
544
563
their overriding functions updated.
545
564
)
546
565
566
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
547
567
---------------
548
568
class Foo
549
569
{
@@ -560,6 +580,7 @@ class Foo2 : Foo
560
580
}
561
581
}
562
582
---------------
583
+ )
563
584
564
585
$(H2 $(LNAME2 static, $(D static) Attribute))
565
586
@@ -570,6 +591,7 @@ $(H2 $(LNAME2 static, $(D static) Attribute))
570
591
$(D static) is ignored when applied to other declarations.
571
592
)
572
593
594
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
573
595
---------------
574
596
class Foo
575
597
{
@@ -585,6 +607,7 @@ Foo.foobar(); // error, no instance of Foo
585
607
f.bar(); // produces 6;
586
608
f.foobar(); // produces 7;
587
609
---------------
610
+ )
588
611
589
612
$(P
590
613
Static functions are never virtual.
@@ -696,9 +719,13 @@ $(GNAME UserDefinedAttribute):
696
719
697
720
A user-defined attribute looks like:
698
721
722
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
699
723
---
700
724
@(3) int a;
701
725
---
726
+ )
727
+
728
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
702
729
---
703
730
@("string", 7) int b;
704
731
@@ -712,6 +739,7 @@ struct Bar
712
739
713
740
@Bar(3) int d;
714
741
---
742
+ )
715
743
716
744
$(P
717
745
If there are multiple UDAs in scope for a declaration, they are concatenated:
@@ -729,10 +757,12 @@ struct Bar
729
757
UDA's can be extracted into an expression tuple using $(D __traits):
730
758
)
731
759
760
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
732
761
---
733
762
@('c') string s;
734
763
pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
735
764
---
765
+ )
736
766
737
767
$(P
738
768
If there are no user-defined attributes for the symbol, an empty tuple is returned.
0 commit comments