On Wednesday, 21 February 2018 at 14:29:31 UTC, Simen Kjærås wrote:
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?

No can do. The solution is to use a factory function:

Feared the same, thanks.

struct Foo(T) {
    static Foo create(int n)(float f) {
        Foo result;
        static foreach( i; 0..n) { do_some_ctfe_magic;}
        return result;
    }
}

--
  Simen

I will consider this, actually I use something quite close, but my create is not static and does not return anything. It simply initializes the struct after it has been constructed with the default ctor. The templated user ctor would have been nice, though.

Reply via email to