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

captaindet 2krnk at gmx.net
Sat Mar 22 15:54:06 PDT 2014


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 );
}


More information about the Digitalmars-d-learn mailing list