Skip to content

Commit

Permalink
[libc++] Fix chrono::duration constructor constraint
Browse files Browse the repository at this point in the history
As per [time.duration.cons]/1, the constructor constraint should be on
const Rep2&. As it is now the code will fail to compile in certain
cases, for example (https://godbolt.org/z/c7fPrcTYM):

     struct S{
          operator int() const&& noexcept = delete;
          operator int() const& noexcept;
     };

     const S &fun();

     auto k = std::chrono::microseconds{fun()};

Differential Revision: https://reviews.llvm.org/D118902
  • Loading branch information
tiagomacarios authored and ldionne committed Feb 3, 2022
1 parent daf82a5 commit eaadc45
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libcxx/include/__chrono/duration.h
Expand Up @@ -251,7 +251,7 @@ class _LIBCPP_TEMPLATE_VIS duration
explicit duration(const _Rep2& __r,
typename enable_if
<
is_convertible<_Rep2, rep>::value &&
is_convertible<const _Rep2&, rep>::value &&
(treat_as_floating_point<rep>::value ||
!treat_as_floating_point<_Rep2>::value)
>::type* = nullptr)
Expand Down
Expand Up @@ -20,24 +20,45 @@
#include "test_macros.h"
#include "../../rep.h"

#if TEST_STD_VER >= 11
struct NotValueConvertible {
operator int() const&& = delete;
constexpr operator int() const& { return 1; }
};
#endif

template <class D, class R>
void
test(R r)
{
TEST_CONSTEXPR_CXX14 void check(R r) {
D d(r);
assert(d.count() == r);
}

TEST_CONSTEXPR_CXX14 bool test() {
check<std::chrono::duration<int> >(5);
check<std::chrono::duration<int, std::ratio<3, 2> > >(5);
check<std::chrono::duration<Rep, std::ratio<3, 2> > >(Rep(3));
check<std::chrono::duration<double, std::ratio<2, 3> > >(5.5);

// test for [time.duration.cons]/1
#if TEST_STD_VER >= 11
constexpr D d2(R(2));
static_assert(d2.count() == 2, "");
check<std::chrono::duration<int> >(NotValueConvertible());
#endif

return true;
}

int main(int, char**)
{
test<std::chrono::duration<int> >(5);
test<std::chrono::duration<int, std::ratio<3, 2> > >(5);
test<std::chrono::duration<Rep, std::ratio<3, 2> > >(Rep(3));
test<std::chrono::duration<double, std::ratio<2, 3> > >(5.5);
int main(int, char**) {
test();
#if TEST_STD_VER > 11
static_assert(test(), "");
#endif

return 0;
// Basic test for constexpr-friendliness in C++11
#if TEST_STD_VER >= 11
{
constexpr std::chrono::duration<int> d(5);
static_assert(d.count() == 5, "");
}
#endif
return 0;
}

0 comments on commit eaadc45

Please sign in to comment.