www.digitalmars.com         C & C++   DMDScript  

c++ - a couple of questions.. (mixins and inout)

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
class A
{
 void func(){}
 template funcdecl(Type)
 {
  void func(Type t){writefln(t);}
 }
 mixin funcdecl!(int) ;
 mixin funcdecl!(float) ;
}

We all know this doesn't work but it can be fixed by adding aliases:
 mixin funcdecl!(int) a1;
 mixin funcdecl!(float) a2;
 alias a1.func func;
 alias a2.func func;

And now the name overloading works, but:
what if i want to mixin constructors, how can i do that?

 template funcdecl(Type)
 {
  this(Type t){}
 }
 mixin funcdecl!(int) a1;
 mixin funcdecl!(float) a2;
 alias a1.this this;
 alias a2.this this;

This doesn't work!

And another question:

class A{}
 void func(inout A a){}
 func(new A);
--> this doesn't work why? (new A  is not an lvalue)
If C++ can do it why not D?

It is a lot more complicated to assign temporary objects
to variables and then pass it as an inout param then to pass
it directly.
Sep 07 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
Sorry! Wrong NG :)

"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:chjmjn$268h$1 digitaldaemon.com...
 class A
 {
  void func(){}
  template funcdecl(Type)
  {
   void func(Type t){writefln(t);}
  }
  mixin funcdecl!(int) ;
  mixin funcdecl!(float) ;
 }

 We all know this doesn't work but it can be fixed by adding aliases:
  mixin funcdecl!(int) a1;
  mixin funcdecl!(float) a2;
  alias a1.func func;
  alias a2.func func;

 And now the name overloading works, but:
 what if i want to mixin constructors, how can i do that?

  template funcdecl(Type)
  {
   this(Type t){}
  }
  mixin funcdecl!(int) a1;
  mixin funcdecl!(float) a2;
  alias a1.this this;
  alias a2.this this;

 This doesn't work!

 And another question:

 class A{}
  void func(inout A a){}
  func(new A);
 --> this doesn't work why? (new A  is not an lvalue)
 If C++ can do it why not D?

 It is a lot more complicated to assign temporary objects
 to variables and then pass it as an inout param then to pass
 it directly.

Sep 07 2004