error : outer function context of `D main` is needed to `new` nested class `main.main.X`

Bert Bert at
Thu Aug 15 01:55:17 UTC 2019

void main()
     class X { ... }
     auto f = foo!X;

then in another module I have a templated function foo that 
simply new's x:

auto foo(T)() { return new T; }

yet I get the error.

I realize that X is local to main and I realize I could do 
something like

foo(new X);

but that completely defeats the purpose(my context is more 
complicated). I need to construct X, not have the user do it.

I thought the templated function would be instantiated at the 
call site?

I've tried to "hack" it in various ways but nothing works. I've 
imported the model, but since X is local to main it can't be 
seen. I've tried to fully qualify it but it can't be found. 
main.main.X says something about void(the return type of main()): 
no property `` for type `void`.

template foo(T)
	alias foo = T;
         alias foo = () { return new T; }; // or

then I can do new foo!T, but I cannot actually construct T like I 
need to. I cannot seem to return a proper type either. I've tried 
returning a lambda and so on, but D is enforcing the type be 
accessible even if the function will be used in the correct 

I don't understand stand why it is a problem when the template 
functions are suppose to be instantiated at the call site.

Seems like a complete pain just to be able to construct a type 
outside it's context(but the construction is totally within the 
context, or should be. I'm simply trying to provide a simple 
wrapper that does some boilerplate and makes everything look 

More information about the Digitalmars-d-learn mailing list