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 22:35:30 PST 2015


I have a template fuction that looks like this:

immutable(T)[] getString(T)() const
if (is(T == dchar)||is(T == wchar)||is(T == char))

Basically, I was hoping that the type would be deduced based on 
the prameter that was being assigned to like so.

string ret = thing.getString();

Apparently that does not work, though. I get informed that the 
type of the template is not able to be deduced. I'm curious as to 
why it was not able to deduce it on its own.

Additionally, and I haven't run my code to try this yet, but 
giving T a default type compiled to my surprise.

immutable(T)[] getString(T=char)() const
if (is(T == dchar)||is(T == wchar)||is(T == char))

Is something like this supposed even to work?


More information about the Digitalmars-d-learn mailing list