opDispatch and template parameters

Simen kjaeraas simen.kjaras at gmail.com
Mon Sep 13 18:04:17 PDT 2010


Guillaume B. <guillaume.b.spam at spam.ca> wrote:

> Hi,
>
>  Is this usage of opDispatch supposed to work:
>
> ====
> module test.d;
>
> import std.stdio;
>
> struct DispatchTest {
>     void opDispatch(string name, string otherName)() {
>         writeln(name, ":", otherName);
>     }
> }
>
> void main() {
>     DispatchTest t;
>     //t.testName!("testOtherName")();
>     t.opDispatch!("testName", "testOtherName")();
> }
> ====
>
>  This compiles fine but if I remove the commented line, dmd (v2.048)  
> tells
> me:
>
> test.d(13): Error: template instance opDispatch!("testName") does not  
> match
> template declaration opDispatch(string name,string otherName)
>
> The error seems OK for a "normal" function, but for opDispatch, it seems
> limiting to me. Is this a bug?

Sorry for not noticing this post before. This is not a bug. The correct
way to do what you want is this:


module foo;

import std.stdio;

struct test {
     template opDispatch( string name ) {
         void opDispatch( string other )( ) {
             writeln( name, ", ", other );
         }
     }
}

void main( ) {
     test t;
     t.foo!( "Hey!" )( );
}

-- 
Simen


More information about the Digitalmars-d mailing list