Template functions, can we make it more simple?
Timon Gehr
timon.gehr at gmx.ch
Fri Aug 2 15:40:19 PDT 2013
On 08/03/2013 12:19 AM, Andrei Alexandrescu wrote:
> On 8/2/13 3:17 PM, Timon Gehr wrote:
>> On 08/03/2013 12:06 AM, Andrej Mitrovic wrote:
>>> On 8/2/13, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
>>>> auto fun(auto x, auto y) { … }
>>>>
>>>> Truth be told, at the time of that decision
>>>> parameter names (viz. x and y) could not be used in template
>>>> constraints. Now they could, so in a way that reopens the question.
>>>
>>> You'd still have to use typeof(x) and typeof(y) to extract the types,
>>> so even though you end up making the template declaration simpler you
>>> also make the constraint more complicated.
>>> ...
>>
>> bool compare(a,b) if(__traits(compiles,a<b)) { return a<b; }
>
> Direct use of __traits is unrecommended outside the stdlib.
The language is not expressive enough to wrap __traits(compiles,a<b) in
the stdlib.
> I had this pattern in mind:
>
> bool compare(auto a, auto b) if (is(typeof(a < b) : bool)) { ... }
> ...
Checking for implicit conversion to bool is redundant due to how
operator overloading is handled for '<'.
Furthermore, this constraint is not sufficient.
bool compare(A,B)(A a,B b) if (is(typeof(a < b) : bool)){
return a<b;
}
void main(){
int o(T)(T r){ return 0; }
static struct S{
alias o opCmp;
}
compare(S(),S()); // causes error in template body
}
DMD's implementation of __traits(compiles,...) currently shares this
issue; this is a compiler bug as far as I can tell. The above behaviour
is to be expected for is(typeof(.)) though.
More information about the Digitalmars-d
mailing list