Possible bug

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 25 17:00:09 PDT 2013


On Mon, 25 Mar 2013 17:13:58 -0400, Ali Çehreli <acehreli at yahoo.com> wrote:

> On 03/25/2013 12:40 PM, Steven Schveighoffer wrote:
>  > On Mon, 25 Mar 2013 11:31:17 -0400, Ali Çehreli <acehreli at yahoo.com>  
> wrote:
>  >
>  >> This design allows templated constructors:
>  >>
>  >> struct S // <-- not a template
>  >> {
>  >> this(T)(T t) // <-- template
>  >> {
>  >> // ...
>  >> }
>  >>
>  >> // ...
>  >> }
>  >>
>  >> The same in C++...
>  >
>  > Templated constructors would not be disallowed if you allowed IFTI on
>  > templated structs/classes without templated constructors.
>
> It would complicate matters: The parameter would be for the constructor  
> if the constructor were a template, for the struct otherwise.

In cases where the struct is templated and the constructor is not, it  
would apply to the struct.  In cases where the struct is concrete and the  
constructor is templated, it would apply to the constructor.  In the case  
where the struct is templated AND the constructor is templated, it would  
require the struct to have an explicit instantiation.

This is not complex, nor unintuitive.

>  > When you decompose constructors, they are simply fucntions, and IFTI
>  > exists on functions. The same should be allowed for constructors.
>
> I completely agree and that's my point. :) The template parameter list  
> of the constructor should stay with the constructor.

In the rare cases where the struct and the constructor are templated, it  
will stay with the constructor.

>  > There is almost no difference between this:
>  >
>  > template foo(T){
>  > void foo(T t) {}
>  > }
>  >
>  > and this:
>  >
>  > struct foo(T){
>  > this(T t) {}
>  > }
>
> Actually, the latter is a shorthand for this:
>
> template S(T)
> {
>      struct S
>      {
>          T t;
>
>          this(U)(U)
>          {}
>      }
> }

No, it is shorthand for this:

template foo(T)
{
    struct foo
    {
       this(T t) {}
    }
}

Note there is only ONE template type.

For your case, the shorthand would be:

struct S(T)
{
    this(U)(U u) {}
}

>
> As you see, T comes from the outer template and U stays with the  
> constructor. It allows the following use:
>
> void main()
> {
>      auto s = S!int(byte.init);
> }

This would be required if both struct and constructor are templated (and  
rightly so).

-Steve


More information about the Digitalmars-d-learn mailing list