On 04/12/2013 04:00 PM, bearophile wrote:
> This is one of the few C++14 core language proposals that seem
> interesting for D:
>
> http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3602.html
>
> The purpose of this idea is to avoid the helper functions that are
> currently used in D to build template structs/classes. Even if this
> feature is restricted to only a subset of all the templated
> structs/classes it seems useful to avoid some boilerplate code.
>
> Is this idea adaptable to D?
>
> Bye,
> bearophile
This has come up multiple times before, most recently on the D.learn
forum. My reaction is that what if the constructor is also a template?
Or what if only the constructor is a template?
To paraphrase Steven Schveighoffer:
- if only the type is a template then the constructor parameters are
used for deducing the template parameters of the type
- if only the constructor is a template, then the constructor parameters
are used for deducing the template parameters of the constructor
- if both are templates then it is an ambiguity
I still think that the code will be confusing. If the proposal is
implemented, the two lines in main below will look the same but will
have different meanings:
import std.stdio;
// Today:
struct S
{
this(T)(T param)
{
writefln("constructing with %s", T.stringof);
}
}
// Proposed:
struct S2(T)
{
this(T param)
{
writefln("constructing S2!%s", T.stringof);
}
}
void main()
{
/* Can we tell by reading the following code whether S or S2 is a
* template, or whether the constructor of one of them is a
* template? Do we care? */
auto s = S(42);
auto s2 = S2(43); // This is proposed; not valid yet.
}
Note that the C++ proposal does not touch on this point.
Ali