Bug 5934 – Finite recursive templates are not allowed
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2011-05-06T06:22:00Z
Last change time
2011-05-06T08:13:33Z
Assigned to
nobody
Creator
maximzms
Comments
Comment #0 by maximzms — 2011-05-06T06:22:45Z
Finite recursion in parametrized structures produces compilation errors.
This kind of recursion could be used to implement custom compile-time expression parsing for user-defined types.
It should be limited but not forbidden.
Example:
----------
struct Foo
{
auto opUnary(string op)()
if(op == "-")
{
return Negation!(typeof(this))(this);
}
}
struct Negation(T)
{
T statement;
auto opUnary(string op)()
if(op == "-")
{
return Negation!(typeof(this))(this);
}
}
void main()
{
Foo a;
auto b = -a; // no errors
auto c = -(-a); /* Error: recursive template expansion for template
* argument Negation!(Foo)
*/
}
----------
Comment #1 by maximzms — 2011-05-06T07:55:47Z
The following code works, but such workarounds are annoying:
----------
struct Foo
{
auto opUnary(string op)()
if(op == "-")
{
return Negation!(typeof(this))(this);
}
}
struct Negation(T)
{
T statement;
auto opUnary(string op)()
if(op == "-")
{
return MakeNeg(this); // The return type is "Negation!(Negation!(T))"
}
}
// Function that makes the recursion implicit
auto MakeNeg(T)(T stat)
{
return Negation!(T)(stat);
}
void main()
{
Foo a;
auto b = -a; // no errors
auto c = -(-a); // no errors
}
----------
Comment #2 by maximzms — 2011-05-06T08:13:33Z
*** This issue has been marked as a duplicate of issue 3869 ***