Default struct constructor

aliak something at something.com
Mon Feb 11 18:36:06 UTC 2019


On Friday, 8 February 2019 at 08:25:18 UTC, Jonathan M Davis 
wrote:
> It's true that there are use cases where the lack of a default 
> constructor for structs is annoying, but in general, all you 
> have to do is use a factory method instead and then make sure 
> that the type still works if its init value is used (e.g. make 
> sure that the destructor is aware that the factory method can 
> be bypassed). And if you absolutely must have default 
> construction, you can always use a class instead.
>
> - Jonathan M Davis

Just found if you need a type that can be parameterized by an 
alias to a function that accesses some local frame data, and you 
want the factory to be static on the type, you can't do it.

Seems like a bug maybe? as I'm not sure what the difference is 
between the static construct function below or a global one that 
is parameterized on "alias fun" ?

struct S(alias fun) {
     static auto construct() {
         auto a = S!fun();
         return a;
     }
     void call() { fun(); }
}

void main() {
     int count;
     auto s = S!(() => count++).construct;
}

onlineapp.d(5): Error: cannot access frame pointer of 
onlineapp.main.S!(delegate () => count++).S



More information about the Digitalmars-d mailing list