Method overloading without Type2Type
Benjamin Thaut
code at benjamin-thaut.de
Tue Apr 26 13:06:50 PDT 2011
Am 26.04.2011 21:55, schrieb Philippe Sigaud:
> On Tue, Apr 26, 2011 at 18:41, Benjamin Thaut<code at benjamin-thaut.de> wrote:
>> Thanks, but that is not connected to my question at all,
>> I want to implement the echo method so that the type is passed as a template
>> argument, and not as a function argument. While that is happening I still
>> want to be able to overload the function.
>> Is this possible in D 2?
>
> I'm not sure I understand what you're trying to do (didn't read
> "Modern C++ design"). Here is something that compiles:
>
> import std.stdio;
>
> class Foo(T,R...) : Foo!(R) {
> public void print(){
> writeln(T.stringof);
> super.print();
> }
>
> public void echo(U)(U u = U.init) {
> writeln(U.stringof);
> }
> }
>
> class Foo(T){
> public void print(){
> writeln("end: " ~ T.stringof);
> }
>
> public void echo(U)(U u = U.init) {
> writeln(U.stringof);
> }
> }
>
> void main(string[] args){
> auto test = new Foo!(int,float,double,short,byte)();
> test.print();
>
> test.echo!double;
> test.echo!short;
> }
>
The Problem with that version is, that the code that is generated looks like
void main(string[] args){
auto test = new Foo!(int,float,double,short,byte)();
test.print();
test.echo!double(double.init);
test.echo!short(short.init);
}
If the types that are used, are no simple data types, but rather large
structs, they are copied on every function call. Thats exactly what I
want to avoid. The Type2Type template has a size of 0, thats why I'm
using that in the first place.
--
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d-learn
mailing list