Template parameter deduction for constructors?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 15 08:18:55 PDT 2013


On Sun, 14 Apr 2013 11:47:47 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:

> On 04/13/2013 01:00 AM, 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
>
> I think the differences to be accounted for are basically that D has  
> n-ary default constructors, static opCall, and static if. Furthermore,  
> with IFTI, it is possible to only specify the prefix of the template  
> arguments.
>
> ---
>
> struct S(T...){
>      T data;
> }
>
> S(1,2,3); // ?

Should work.  The implicit default constructor should be treated as a  
function.

> S!double(1,2,3); // ?

Should work with T == {double, int, int}

>
> ---
>
> struct S(T...){
>      T data;
>      static if(T.length){
>          this(T x){ }
>      }
> }
>
> S(1,2,3); // ?

I think this should follow IFTI rules.  If this is allowed:

template foo(T...) {
    static if(T.length)
       void foo(T x) {}
}

Then the struct should be allowed as well.

>
> ---
>
> struct S(T...){
>      static opCall(T)(T args){ }
> }
>
> S(1,2,3); // ?

I would say it should work.

-Steve


More information about the Digitalmars-d mailing list