Robert Clipsham wrote:
On 08/03/10 22:03, bearophile wrote:
2) What's the best way to translate this to the new operator regime?

T foo(T)(T s) if (__traits(hasMember, T, "opAdd")) {
     return s + s;
}

I have not found a good solution yet. This solution looks buggy (also because fixed-sized arrays have a buggy .init):
-snip-
T foo(T)(T s) if (__traits(compiles, {return T.init + T.init;})) {
     return s + s; // line 14
}

Untested, will the following do what you need?

----
T foo(T)(T s) if (__traits(compiles, {return s + s;})) {
     return s + s;
}
----

Seems like you may as well test if you can add what you're passed rather than the initial value for the type.

What I usually do is:

T foo(T)(T s) if (is(typeof(s + s))) {
}

Andrei

Reply via email to