Deducing a template retrun parameter type based on an assignment?
Jeremy DeHaan via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jan 29 23:13:08 PST 2015
On Friday, 30 January 2015 at 06:58:58 UTC, Vlad Levenfeld wrote:
> On Friday, 30 January 2015 at 06:35:31 UTC, Jeremy DeHaan wrote:
>>
>>A bunch of stuff
>>
>
> for template type deduction to work, you have to supply an
> argument. Your type signature would need to look like this:
>
> immutable(T)[] getString(T)(T arg) const
>
> and then T would be deduced from arg.
That seems strange. I figured that it would be smart enough to
deduce the parameter type based on the type that it was trying to
be assigned to.
>
> But string ret = thing.getString(); won't compile because it is
> rewritten to getString(thing), but your getString function
> takes no runtime parameters. It looks like the signature I
> wrote earlier is what you actually want.
Whoops. I should have mentioned that this is a member function in
a class and not a free fiction. Not that it changes much about
the deduction.
>
> As to your second example, it'll work fine. Basically your
> signature says "only accept dchar, wchar or char, but if
> nothing's been specified, default to char". But if you rewrite
> getString to take one parameter, then the default template arg
> is redundant.
That is good to hear. It seemed like that was the way it would
work, but I've never had to specify a default template parameter
type. I'm hoping to avoid having to specify a template parameter,
but it seems like it can't be helped if users want to get their
string type as a wstring or dstring though.
More information about the Digitalmars-d-learn
mailing list