[libcxx-commits] [PATCH] D57778: std::abs should not return double (2735)
Zoe Carver via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Feb 10 14:55:55 PST 2019
zoecarver marked 16 inline comments as done.
zoecarver added inline comments.
================
Comment at: test/std/numerics/c.math/abs.pass.cpp:27
+
+ ASSERT_SAME_TYPE(decltype(std::abs(neg_val)), R);
+
----------------
EricWF wrote:
> zoecarver wrote:
> > zoecarver wrote:
> > > mclow.lists wrote:
> > > > mclow.lists wrote:
> > > > > EricWF wrote:
> > > > > > I think this test for the return type can be simplified. We expect `std::abs(T)` to return `U` where `U` is the result of [integral promotion](https://en.cppreference.com/w/cpp/language/implicit_conversion) on `T`.
> > > > > >
> > > > > > Instead of hard-coding the expected type, we can calculate it using the expression `using R = decltype(+pos_value);`
> > > > > I suggested that Zoe be explicit here.
> > > > In particular, we want to be sure that `std::abs(char)` returns `int`, not `char`, which can be promoted to an `int`.
> > > >
> > > I will play around with this a bit and see what works. Is there a template for integral promotion?
> > Do either of you know how to do something like this?
> >
> > ```
> > template <typename T, typename U>
> > struct upgrade_intergal
> > {
> > using type = typename std::conditional<sizeof(T) <= sizeof(U), U, upgrade_intergal<T, long U>>::type;
> > };
> > ```
> @mclow.lists Only if `char` is signed. But yes.
I think I am going to leave it the way it was originally for now (I like the explicitness of it anyway).
================
Comment at: test/std/numerics/c.math/abs.pass.cpp:40
+
+int main(int, char**)
+{
----------------
EricWF wrote:
> EricWF wrote:
> > zoecarver wrote:
> > > mclow.lists wrote:
> > > > EricWF wrote:
> > > > > `int main()` is fine. Same with omitting `return 0;`.
> > > > Not with the new freestanding stuff that Louis is working on
> > > @EricWF Is this preferred?
> > Fudge me, really? OK. Nevermind then.
> Woops. My bad. I didn't know we are switching how we declare main. Ignore me.
Oh, didn't see your previous comment -- sorry.
================
Comment at: test/std/numerics/c.math/abs.pass.cpp:54
+ test_abs<std::int32_t, int>();
+ test_abs<std::int64_t, long long>();
+
----------------
mclow.lists wrote:
> EricWF wrote:
> > This is non-portable. AFAIK there is no reason why `int64_t` can't be `long` on 64 bit platforms.
> > Using the integral promotion approach avoids this.
> this is true.
I fixed this by expecting `int64_t `.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D57778/new/
https://reviews.llvm.org/D57778
More information about the libcxx-commits
mailing list