shouldn't this work? / how to make it work?

anonymous anonymous at example.com
Sat Mar 22 17:37:58 PDT 2014


On Saturday, 22 March 2014 at 22:54:15 UTC, captaindet wrote:
> pls see example code below.
>
> the two 'test' templates work fine by themselves. if, however, 
> in the same module, the eponymous template does not work 
> anymore. instead the compiler seems to try instantiating the 
> variadic template.
>
> a) why? for how i understand it, this should not happen as
> o they have very different signatures, i.e. completely 
> incompatible argument lists ( zero vs >1 arguments)
> o the string parameter is more specialized than the take-all 
> tuple
>
> b) how can i make it work?
>
> cheers,
> det
>
> CODE:
> =====
>
> import std.stdio;
>
> // cannot be used with function arguments
> template test(string str ){
> 	enum test = "template 'test' used with "~str;
> }
>
> // cannot be called with less than 2 function arguments
> string test (T ...)( T strs )
> 	if( 1<strs.length )
> {
> 	return "templated function 'test' called with "~strs[0]~" and 
> "~strs[1];
> }
>
> // enum epo = test!"one";			// use eponymous template
> 	// if commented in:
> 	// Error: tuple T is used as a type
> 	// in line 9 = string test (T ...)( T strs )
>
> enum vari = test("two", "three");	// use templated/variadic 
> function
>
> void main(string[] args)
> {
> 	// writeln( epo );
> 	writeln( vari );
> }

Looks like a bug to me.

By the way, the variadic template is eponymous, too: The template
declares a function with the same name.


More information about the Digitalmars-d-learn mailing list