[libc++] Fix chrono::duration constructor constraint

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
This commit is contained in:
Tiago Macarios 2022-02-03 10:23:15 -05:00 committed by Louis Dionne
parent daf82a51a0
commit eaadc45156
2 changed files with 34 additions and 13 deletions

View File

@ -251,7 +251,7 @@ public:
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)

View File

@ -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;
}