Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Issue 8687 - Variadic templates do not work properly with default…
… arguments
- Loading branch information
1 parent
15c8da1
commit 36af6b1
Showing
3 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Function parameters with default values are now allowed after variadic template parameters, and always take their default values. This allows using special tokens (eg __FILE__) after variadic parameters, which was previously impossible | ||
|
||
Eg: | ||
--- | ||
string log(T...)(T a, string file = __FILE__, int line = __LINE__) | ||
{ | ||
return text(file, ":", line, " ", a); | ||
} | ||
|
||
assert(log(10, "abc") == text(__FILE__, ":", __LINE__, " 10abc")); | ||
--- | ||
|
||
This should be preferred to the previous workaround, which caused template bloat: | ||
--- | ||
string log(string file = __FILE__, int line = __LINE__, T...)(T a); | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
PERMUTE_ARGS: | ||
*/ | ||
|
||
import std.typecons : tuple; | ||
import std.conv : text; | ||
|
||
void fun0(U, T...)(U gold, int b_gold, T a, int b) | ||
{ | ||
assert(tuple(a) == gold); | ||
assert(b == b_gold); | ||
} | ||
|
||
void fun(U, T...)(U gold, T a, int b = 1) | ||
{ | ||
assert(tuple(a) == gold); | ||
assert(b == 1); | ||
} | ||
|
||
void fun2(U, V, T...)(U gold, V gold2, T a, string file = __FILE__, int line = __LINE__) | ||
{ | ||
assert(tuple(a) == gold); | ||
assert(tuple(file, line) == gold2); | ||
} | ||
|
||
// | ||
void fun3(int[] gold, int[] a...) | ||
{ | ||
assert(gold == a); | ||
} | ||
|
||
/+ | ||
NOTE: this is disallowed by the parser: | ||
void fun4(int[] gold, int[] a ..., int b = 1) | ||
{ | ||
assert(gold==a); | ||
assert(b==1); | ||
} | ||
+/ | ||
|
||
// Example in changelog | ||
string log(T...)(T a, string file = __FILE__, int line = __LINE__) | ||
{ | ||
return text(file, ":", line, " ", a); | ||
} | ||
|
||
void main() | ||
{ | ||
fun0(tuple(10), 7, 10, 7); | ||
|
||
fun(tuple()); | ||
fun(tuple(10), 10); | ||
fun(tuple(10, 11), 10, 11); | ||
|
||
fun2(tuple(10), tuple(__FILE__, __LINE__), 10); | ||
|
||
fun3([1, 2, 3], 1, 2, 3); | ||
// fun4([1,2,3], 1,2,3); | ||
|
||
assert(log(10, "abc") == text(__FILE__, ":", __LINE__, " 10abc")); | ||
} |