Template Question

Mike Parker aldacron71 at yahoo.com
Mon Apr 14 19:17:39 PDT 2008


Bill Baxter wrote:
> Mike Parker wrote:
>> Bill Baxter wrote:
>>> Mike Parker wrote:
>>>> I've not worked much with templates in any language beyond the 
>>>> basics. Right now, I've got a problem that I've solved, but for 
>>>> which I'm looking a different solution.
>>>>
>>>> What I have is a templated interface like so:
>>>>
>>>> interface Foo(T)
>>>> {
>>>>     void init(T);
>>>>     void term();
>>>>     void blah();
>>>>     void bleh();
>>>> }
>>>>
>>>> Then I have a Templated manager class intended to work with 
>>>> differently parameterized Foos:
>>>>
>>>> class Manager(U)
>>>> {
>>>>     ...
>>>> }
>>>>
>>>> The issue is that I want to make sure that Manager is instantiated 
>>>> with Foos without it knowing what the T in Foo is. This obviously 
>>>> doesn't work:
>>>>
>>>> class Manager(U : Foo)
>>>> {
>>>> }
>>>
>>> I think there is a variation of that which is supposed to work:
>>>
>>> class Manager(U : Foo!(T), T)
>>> {
>>> }
>>>
>>> I'm not sure if it does in practice or not, though.
>>>
>>>
>>
>> Unfortunately, it doesn't.
> 
> I keep forgetting you don't get any type deduction with class templates.
> So maybe would work if you are explicit about both arguments.  But that 
> kind of defeats the purpose.
> 
> Yeh, so I guess you just have to settle for
> 
> class Manager(U /* : Foo!(T) */)
> {
>    static if (is(U T_ : Foo!(T_))) {
>       alias T_ T;
>    }
>    else {
>       static assert(false, "U must be a Foo!(T) for some T");
>    }
> }
> 
> --bb

This is exactly what I was looking for. I don't understand it, but it works.


More information about the Digitalmars-d-learn mailing list