Function template parameter inference from class template

Jarrett Billingsley jarrett.billingsley at gmail.com
Wed Sep 23 15:39:19 PDT 2009


On Wed, Sep 23, 2009 at 6:17 PM, Justin Johansson
<procode at adam-dott-com.au> wrote:
>
> Wow Jarret!!!  So many words on my part to explain what I wanted and you came up with this just so, so coolly concise solution.  It doesn't exactly look like a textbook solution so me thinks I can forgive myself for not figuring it out. (hey only 3 weeks into D now).
>
> I don't know if it would be pushing my luck or not, but is your concept generalizable to more parameters.  In particular I want to be able to extend this so than bar() can return a generic type.
>
> So now I have this:
>
> class Foo(T)
> {}
>
> T2 bar(T : Foo!(U), U)(T t)
> {
>  T2 x = ...
>  return x;
> }
>
> void main()
> {
>        auto foo = new Foo!(float)();
>        auto chu = bar!(double, float)( foo);
>       // type of chu is double
> }
>
> and by analogy with the first problem I would like to instantiate like so:
>
> void main()
> {
>        auto foo = new Foo!(float)();
>        auto chu = bar!(double)( foo);    // be nice if float could be deduced from foo parameter
>       // type of chu is double
> }
>

class Foo(T)
{}

R bar(R, T : Foo!(U), U)(T t)
{
	R r;
	return r;
}

void main()
{
	auto foo = new Foo!(float)();
	auto chu = bar!(double)(foo);
	pragma(msg, typeof(chu).stringof);
}

Make sure you have a newer compiler, though, as partial template
specialization with IFTI was only introduced in DMD 1.038.



More information about the Digitalmars-d mailing list