www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unexpected type names with template typedefs

reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
struct foo(T)
{
	T value;
}

template bar(T)
{
	typedef foo!(T) bar;
}

void main(string[] args)
{
	writefln((bar!(int)).stringof); // prints 'bar'
	writefln((bar!(float)).stringof); // prints 'bar'
	writefln((bar!(bar!(int))).stringof); // prints 'bar'

	writefln((foo!(int)).stringof); // prints 'foo!(int)'
	writefln((foo!(float)).stringof); // prints 'foo!(float)'
	writefln((foo!(foo!(int))).stringof); // prints 'foo!(foo!(int))'
}


Should not the output of the first three be 'bar!(int)', 'bar!(float)',  
and 'bar!(bar!(int))'?

-- Simen
Apr 28 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Simen,

 struct foo(T)
 {
 T value;
 }
 template bar(T)
 {
 typedef foo!(T) bar;
 }
 void main(string[] args)
 {
 writefln((bar!(int)).stringof); // prints 'bar'
 writefln((bar!(float)).stringof); // prints 'bar'
 writefln((bar!(bar!(int))).stringof); // prints 'bar'
 writefln((foo!(int)).stringof); // prints 'foo!(int)'
 writefln((foo!(float)).stringof); // prints 'foo!(float)'
 writefln((foo!(foo!(int))).stringof); // prints 'foo!(foo!(int))'
 }
 Should not the output of the first three be 'bar!(int)',
 'bar!(float)',  and 'bar!(bar!(int))'?
 

you might be getting a smaller case than what you want, namely the typedef rater than the name of the template. this might be be related to fully qualified names and such.
Apr 28 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 29 Apr 2008 05:45:18 +0200, BCS <ao pathlink.com> wrote:

 Reply to Simen,

 struct foo(T)
 {
 T value;
 }
 template bar(T)
 {
 typedef foo!(T) bar;
 }
 void main(string[] args)
 {
 writefln((bar!(int)).stringof); // prints 'bar'
 writefln((bar!(float)).stringof); // prints 'bar'
 writefln((bar!(bar!(int))).stringof); // prints 'bar'
 writefln((foo!(int)).stringof); // prints 'foo!(int)'
 writefln((foo!(float)).stringof); // prints 'foo!(float)'
 writefln((foo!(foo!(int))).stringof); // prints 'foo!(foo!(int))'
 }
 Should not the output of the first three be 'bar!(int)',
 'bar!(float)',  and 'bar!(bar!(int))'?

you might be getting a smaller case than what you want, namely the typedef rater than the name of the template. this might be be related to fully qualified names and such.

That's what I think as well. I'm just wondering if it is correct. Seeing as the typedef maps to a template instantiation that would normally be called bar!(Something), I'd expect the typedef to match that name. -- Simen
Apr 28 2008
parent BCS <BCS pathlink.com> writes:
Simen Kjaeraas wrote:
 BCS wrote:
 
 you might be getting a smaller case than what you want, namely the  
 typedef rater than the name of the template. this might be be related 
 to  fully qualified names and such.

That's what I think as well. I'm just wondering if it is correct. Seeing as the typedef maps to a template instantiation that would normally be called bar!(Something), I'd expect the typedef to match that name. -- Simen

I think you might be in undefined territory. Not that it should be undefined though. IMHO what you are doing /should/ work. p.s. I'd like to see this as well typedef foo!(T) bar(T); or if the args on the right looks to ugly: typedef(T) foo!(T) bar;
Apr 29 2008