Skip to content

Commit a1b9a22

Browse files
authored
Merge pull request #3424 from ntrel/nested-limit
[spec/template] Improve nested template docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents 9ca53f3 + 67f7859 commit a1b9a22

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

spec/template.dd

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ $(H2 $(LNAME2 nested-templates, Nested Templates))
14941494
instantiated functions will implicitly capture the context of the
14951495
enclosing scope.)
14961496

1497+
$(SPEC_RUNNABLE_EXAMPLE_RUN
14971498
----
14981499
class C
14991500
{
@@ -1525,11 +1526,46 @@ $(H2 $(LNAME2 nested-templates, Nested Templates))
15251526
assert(c.num == 10);
15261527
}
15271528
----
1529+
)
15281530

1529-
$(P Above, $(D Foo!().foo) will work just the same as a member function
1531+
$(P Above, $(D Foo!().foo) will work just the same as a `final` member function
15301532
of class $(D C), and $(D Bar!().bar) will work just the same as a nested
15311533
function within function $(D main$(LPAREN)$(RPAREN)).)
15321534

1535+
$(H3 $(LNAME2 limitations, Aggregate Type Limitations))
1536+
1537+
$(P A nested template cannot add non-static fields to an aggregate type.
1538+
Fields declared in a nested template will be implicitly `static`.)
1539+
$(P A nested template cannot add virtual functions to a class or interface.
1540+
Methods inside a nested template will be implicitly `final`.)
1541+
1542+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1543+
------
1544+
class Foo
1545+
{
1546+
template TBar(T)
1547+
{
1548+
T xx; // becomes a static field of Foo
1549+
void func(T) {} // implicitly final
1550+
//abstract void baz(); // error, final functions cannot be abstract
1551+
1552+
static T yy; // Ok
1553+
static void func(T t, int y) {} // Ok
1554+
}
1555+
}
1556+
1557+
void main()
1558+
{
1559+
alias bar = Foo.TBar!int;
1560+
bar.xx++;
1561+
//bar.func(1); // error, no this
1562+
1563+
auto o = new Foo;
1564+
o.TBar!int.func(1); // OK
1565+
}
1566+
------
1567+
)
1568+
15331569
$(H3 $(LNAME2 implicit-nesting, Implicit Nesting))
15341570

15351571
$(P If a template has a $(RELATIVE_LINK2 aliasparameters, template alias parameter),
@@ -1753,26 +1789,6 @@ $(GNAME Constraint):
17531789
auto y = Bar!double; // Error, double does not satisfy constraint
17541790
---
17551791

1756-
$(H2 $(LNAME2 limitations, Limitations))
1757-
1758-
$(P Templates cannot be used to add non-static fields or
1759-
virtual functions to classes or interfaces.
1760-
For example:)
1761-
1762-
------
1763-
class Foo
1764-
{
1765-
template TBar(T)
1766-
{
1767-
T xx; // becomes a static field of Foo
1768-
int func(T) { ... } // non-virtual
1769-
1770-
static T yy; // Ok
1771-
static int func(T t, int y) { ... } // Ok
1772-
}
1773-
}
1774-
------
1775-
17761792
$(SPEC_SUBNAV_PREV_NEXT operatoroverloading, Operator Overloading, template-mixin, Template Mixins)
17771793
)
17781794

0 commit comments

Comments
 (0)