Even when the expression is complete garbage, for example:
---- Example1 ---
struct Foo(int x = "abc" + undefined)
{
}
Foo!(7) w;
-----------------
compiles without errors.
Semantic is run on the default parameters only when they are used.
There are some legitimate reasons to allow the semantic pass to be run late:
--- Example2 ---
struct Foo( T, int x = T.sizeof)
{}
-----------------
but all the spec says is that
"TemplateParameter specializations and default values are evaluated in the scope of the TemplateDeclaration. "
It does not say WHEN they are evaluated.
Further, it says "TemplateInstantances (sic!) are always performed in the scope of where the TemplateDeclaration is declared, with the addition of the template parameters being declared as aliases for their deduced types."
so that the only opportunity for the TemplateDefaultExpression is become valid is via the template parameters.
Checking if the default value is valid is probably not very different from checking if a template body is valid, which is beyond the capabilities of DMD at present.
My feeling is the spec should explicitly state that example2 is legal.
Comment #1 by verylonglogin.reg — 2014-07-13T10:17:05Z
*** Issue 9934 has been marked as a duplicate of this issue. ***
Comment #2 by verylonglogin.reg — 2014-07-13T10:17:45Z
Also one can see discussion of duplicate Issue 9934.
Comment #3 by robert.schadek — 2024-12-13T18:04:48Z