limiting templates

BCS ao at pathlink.com
Tue May 1 20:27:17 PDT 2007


Reply to Daniel,

> BCS wrote:
> 
>> I have a template
>> 
>> int Foo(T)(T bar);
>> 
>> I want to be able to call it like this:
>> 
>> foo("hello");
>> 
>> and have T end up as char[] not char[5]. Is there any way to limit
>> the template to that?
>> 
> Not one that I can think of off the top of my head.  The problem is
> that you can't specialise templated functions.
> 

Am I not understanding you correctly, I thought this worked (it did the last 
time I checked)

int bar(char[] s: "hello")()
{
return s.length;
}

I though of that, but what I going for is code folding to reduce duplication.

I just though of what to do

int FooT(T)(T bar);
alias FooT!(char[]) Foo;
alias FooT!(int) Foo;

this works because I have a finite list of type that are to be allowed.

The "good" way to do it would be to allow for limiting rules to be applied:

int foo(T : is(T[]) || !is(T.length)]) ;




More information about the Digitalmars-d-learn mailing list