limiting templates
Daniel Keep
daniel.keep.lists at gmail.com
Tue May 1 16:59:08 PDT 2007
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.
That said, you might be able to do something like this (note: untested,
off the top of my head code):
template Foo(T)
{
static if( IsStaticArray!(T) )
alias Foo!(DynamicArrayFromStaticArray!(T)) Foo;
else
int Foo(T bar)
{
...
}
}
Obviously, you'd have to write IsStaticArray and
DynamicArrayFromStaticArray (unless they're in std.traits).
The other thing you can do is ensure the argument is really a char[] in
the first place:
foo("hello"[]);
The [] does a slice of the char[5], which results in a char[].
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d-learn
mailing list