D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2733 - Unclear semantics of template value parameters
Summary: Unclear semantics of template value parameters
Status: RESOLVED DUPLICATE of issue 2257
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2009-03-15 04:59 UTC by Max Samukha
Modified: 2014-04-18 09:12 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Max Samukha 2009-03-15 04:59:51 UTC
The following code compiles but the generated binary is not correct.

import std.stdio;

void foo(string s)()
{
    writefln(s);
}

void main()
{
    string s = "test";
    foo!(s);
}
----
Prints nothing. In less trivial cases, passing non-const arguments to value parameters causes data corruption/access violations.

It is not defined whether the argument should be passed by alias or the code should fail to compile complaining about the argument not being evaluatable at compile time.
Comment 1 Stewart Gordon 2009-04-01 04:53:17 UTC
Non-alias template arguments are, by definition, compile-time constants.  s isn't a compile-time constant - it's a mutable reference to immutable data.  So this shouldn't compile.
Comment 2 Max Samukha 2009-04-01 05:13:11 UTC
I'm not that sure anymore. Actually, I wouldn't mind if they were passed by alias. Then, I could write simply

template foo(string s)
{}

instead of

template foo(alias s) if (isString!(s))
{}

If I want to restrict template arguments to statically known values, I can use a isCompileTime constraint

template foo(string s) if (isCompileTime!(s))
{
}

Comment 3 Stewart Gordon 2009-04-01 07:05:32 UTC
(In reply to comment #2)
> If I want to restrict template arguments to statically known values, I can use
> a isCompileTime constraint

Templates with non-alias parameters are instantiated by value at compile-time in the first place.  What you're proposing would be a radical change to this.

You could try defining that such a template may be instantiated either by value or by alias.  However, this is effectively creating two mutually incompatible templates, and it can be confusing to try to work out which is being instantiated.

I can't think of a practical use case for passing a string by alias in a function template, but I'm guessing there's a use for it in templates of some kinds.  The question is whether there are enough use cases for syntactic sugar to be worthwhile, but it could look something like
    template foo(string alias s)
....
Comment 4 Christian Kamm 2009-06-28 00:14:00 UTC
I changed the version to a D1 one as it is also accepts-invalid there.
Comment 5 Walter Bright 2012-01-23 00:28:38 UTC
Not a spec issue - it's a D1 only bug where a template value parameter that is not a compile-time constant is allowed.
Comment 6 yebblies 2012-01-29 22:25:10 UTC

*** This issue has been marked as a duplicate of issue 2257 ***