[libcxx-commits] [PATCH] D67900: [libc++] Use builtin type traits whenever possible
Zoe Carver via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 8 10:26:54 PDT 2019
zoecarver marked an inline comment as done.
zoecarver added inline comments.
================
Comment at: libcxx/include/type_traits:550
+template <class _Tp, class _Up>
+using is_same = _BoolConstant<__is_same(_Tp, _Up)>;
+
----------------
rsmith wrote:
> zoecarver wrote:
> > rsmith wrote:
> > > Nice idea. Is this conforming? Consider this somewhat-contrived example:
> > >
> > > ```
> > > template<typename T> struct A {
> > > void f(is_same<T, int>) {}
> > > void f(is_same<T, float>) {}
> > > void f(false_type) {}
> > > void g() { f(disjunction<is_same<T, int>, is_same<T, float>, false_type>()); }
> > > };
> > > ```
> > >
> > > With a class template `is_same`, `g()` calls the version of `f` corresponding to the "right" option, but with the alias template version, instantiating `A<T>` is ill-formed because multiple signatures of `f` collide.
> > Arg, you are correct :)
> >
> > The following seems to work, though:
> > ```
> > template<bool _V>
> > struct m_bool_constant : integral_constant<bool, _V> { };
> >
> > template <class _Tp, class _Up>
> > using m_is_same = m_bool_constant<__is_same(_Tp, _Up)>;
> >
> > template<typename T> struct A {
> > void f(m_is_same<T, int>) {}
> > void f(m_is_same<T, float>) {}
> > void f(false_type) {}
> > void g() { f(disjunction<m_is_same<T, int>, m_is_same<T, float>, false_type>()); }
> > };
> >
> > A<int> a{ };
> > ```
> >
> > What do you think of the above code?
> I think that still fails for `A<char>` (or for `A<int>` if we add a third `f(m_is_same<...>)` overload).
>
> To strictly conform, I think we need `is_same<A, B>` to result in distinct types for all distinct (`A`, `B`) pairs. :-(
Alrighty, thanks for pointing that out. I think it's still faster to do the following:
```
template <class _Tp, class _Up>
struct m_is_same : integral_constant<bool, __is_same(_Tp, _Up)> { };
```
And that would satisfy the requirement of two distinct template types.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D67900/new/
https://reviews.llvm.org/D67900
More information about the libcxx-commits
mailing list