How to instantiate a template struct with a template constructor without relying on auto deduction?

ixid nuaccount at gmail.com
Wed Feb 21 14:29:38 UTC 2018


On Wednesday, 21 February 2018 at 14:11:10 UTC, ParticlePeter 
wrote:
> struct Foo(T) {
>   T bar;
>   this(S)(S s) {
>     bar = convert(s);
>   }
> }
>
> auto foo = Foo!int(some_float);
>
>
> this works because S is deduced as typeof(some_float), but how 
> would I instantiate the struct without relying on auto 
> deduction?
>
> Suppose we would have this kind of constructor where auto 
> deduction is not possible:
>
>   this(int n)(float f) {
>     static foreach( i; 0..n) { do_some_ctfe_magic;}
>   }
>
> How to instantiate Foo then?

I do not understand what is happening here, I tried to wrote what 
I thought would be the answer. If someone could explain that 
would be great. I wrote this code:

struct Foo2(T, S) {
   T bar;
   this(S s) {
     bar = s.to!T;
   }
}

void main() {
	float some_float = 0.5f;
	int some_int = 1;

	auto foo1 = Foo2!(int, float)(some_float);    // Compiles, OK!
	auto foo2 = Foo2!(int, float)(some_int);      // Compiles, wat?
}



More information about the Digitalmars-d-learn mailing list