www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shouldn't this work? / how to make it work?

reply captaindet <2krnk gmx.net> writes:
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 );
}
Mar 22 2014
parent reply "anonymous" <anonymous example.com> writes:
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.
Mar 22 2014
parent captaindet <2krnk gmx.net> writes:
On 2014-03-22 19:37, anonymous wrote:
 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.
i thought so too
 By the way, the variadic template is eponymous, too: The template
 declares a function with the same name.
right filed a bug report: https://d.puremagic.com/issues/show_bug.cgi?id=12447
Mar 23 2014