Need for std.meta.isSame over __traits(isSame)
user1234
user1234 at 12.de
Wed Sep 1 23:12:56 UTC 2021
On Wednesday, 1 September 2021 at 22:51:40 UTC, Per Nordlöw wrote:
> Can somebody explain the need for
>
> ```d
> private template isSame(alias a, alias b)
> {
> static if (!is(typeof(&a && &b)) // at least one is an
> rvalue
> && __traits(compiles, { enum isSame = a == b; }))
> // c-t comparable
> {
> enum isSame = a == b;
> }
> else
> {
> enum isSame = __traits(isSame, a, b);
> }
> }
> ```
>
> when there is already
>
> ```d
> __traits(isSame, a, b)
> ```
>
> ?
>
> Are there any cases where
>
> ```d
> __traits(isSame, a, b)
> ```
>
> doesn't have the same value as
>
> ```d
> a == b
> ```
>
> provided the static if expression above is true.
the traits does not work on literals passed by
_AliasTemplateParameter_.
```d
enum isSame1(alias a, alias b) = a == b;
enum isSame2(alias a, alias b) = __traits(isSame, a, b);
pragma(msg, isSame1!(0, 0)); // true
pragma(msg, isSame2!(0, 0)); // false
```
This looks a bit like a bug, because `__traits(isSame, 0, 0)`
yields `true`
More information about the Digitalmars-d-learn
mailing list