[Issue 8850] Nested struct creation by a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 19 13:14:59 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8850


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #2 from monarchdodra at gmail.com 2012-10-19 13:14:58 PDT ---
(In reply to comment #0)
> I'm not sure the intended behavior but currently is inconsistent. The following
> code fails to compile with:
> 
> bad.d(2): Error: function D main is a nested function and cannot be accessed
> from bad.fun!(R).fun
> 
> T fun(T)() if(is(T == struct)) {
>     T s;
>     return s;
> }
> 
> void main() {
>     struct R {
>         void f() { }
>     }
> 
>     auto m = fun!R();
> }
> 
> However removing the function from the struct definition (include other values
> if desired) then it will compile. I'd think we'd want templates to have the
> ability to create a nested struct.

Nested structs keep a frame pointer (or something alike) to be able to access
anything inside main, from outside of main. As such, you can't use them (as is)
as a template parameter.

HOWEVER, you can declare your struct as "static" explicitly stating that the
struct does not keep any extra info, at which point it becomes useable:

T fun(T)() if(is(T == struct)) {
   T s;
   return s;
}

void main() {
   static struct R {
       void f() { }
   }

   auto m = fun!R();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list