return type and templates
Andrea Fontana
nospam at example.com
Fri Nov 22 06:01:46 PST 2013
On Friday, 22 November 2013 at 13:43:59 UTC, Jonathan M Davis
wrote:
> On Friday, November 22, 2013 14:29:46 Timon Gehr wrote:
>> The request would be reasonable if 'value' was declared as
>> follows though:
>>
>> @property T value(T)() if (is(T == int)) { return _intValue; }
>>
>> i.e. the fact that the template argument equals the type of the
>> resulting call can be read off directly from the signature.
>> This is in
>> the same ballpark as the existing IFTI features.
>
> How so? IFTI works by inferring the template arguments from the
> function
> arguments. In this case, for the compiler to figure out a type
> that it could
> use to instantiate the template, it would have to disect the
> template
> constraint, which is compeletly different. Yes, this particular
> template
> constraint is very simplistic, but the compiler doesn't even
> look at the
> template constraint until it has a type to test with it, and in
> most cases, it
> would have no way of inferring what types might work even if it
> did look.
>
> Trying to get the compiler to infer T for value would be a
> drastic change to
> how it deals with templates, and at best, it would be able to
> figure out what
> to do in only the most simplistic of cases. In fact, the only
> cases that it
> could figure it out would very simplistic cases where there was
> only one
> possible answer, and if there's only one type that will work
> with a template,
> then there really wasn't much point in templatizing it in the
> first place. And
> as soon as there are multiple types which could work with a
> template, the
> compiler couldn't figure out the correct type no matter how
> smart it was,
> because it would need a way of choosing which of the options to
> take. e.g.
>
> template foo(T)
> if(is(T == int) || is(T == byte))
> {
> ...
> }
>
> Should foo be instantiated with int or byte? Both are equally
> valid.
>
> The OP has come up with a contrived example where it seems
> obvious to him what
> type the compiler should use to instantiate a template which
> has been given no
> template arguments and no function arguments to infer the
> template arguments
> from. But the compiler has no plumbing for figuring out such a
> thing, and
> adding such plumbing would be pointless, because it would only
> work in
> contrived cases such as this one where there was no point in
> templatizing the
> function in the first place.
>
> - Jonathan M Davis
Timon was right, I mean T as return type.
Too bad it needs a drastic change :\
More information about the Digitalmars-d-learn
mailing list