// Copyright (C) 2020-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// .
// { dg-do compile { target c++11 } }
// Test the changes introduced by P0548R1 "common_type and duration".
// Specifically, duration::period should be the reduced ratio,
// and common_type::type should be a duration using the
// reduced ratio.
#include
using std::chrono::duration;
using std::ratio;
using std::common_type;
using std::is_same;
void
test01()
{
using D1 = duration>;
static_assert( is_same>::value,
"duration::period is P::type, not P" );
using C1 = common_type::type;
static_assert( is_same>::value,
"common_type_t is duration");
static_assert( is_same::type, C1>::value,
"common_type_t is common_type_t" );
static_assert( is_same::type, C1>::value,
"common_type_t is common_type_t" );
using D2 = duration>;
static_assert( is_same>::value,
"duration::period is P2::type, not P2" );
using C2 = common_type::type;
static_assert( is_same>::value,
"common_type_t is duration");
static_assert( is_same::type, C2>::value,
"common_type_t is common_type_t" );
static_assert( is_same::type, C2>::value,
"common_type_t is common_type_t" );
using D3 = duration>;
static_assert( is_same>::value,
"duration::period is P3::type, not P3" );
using C3 = common_type::type;
static_assert( is_same>::value,
"common_type_t is duration");
static_assert( is_same::type, C3>::value,
"common_type_t is common_type_t" );
static_assert( is_same::type, C3>::value,
"common_type_t is common_type_t" );
using C12 = common_type::type;
static_assert( is_same::value,
"common_type_t uses the right period" );
using C21 = common_type::type;
static_assert( is_same::value,
"common_type_t is common_type_t" );
using C13 = common_type::type;
static_assert( is_same::value,
"common_type_t uses the right period" );
using C31 = common_type::type;
static_assert( is_same::value,
"common_type_t is common_type_t" );
using C23 = common_type::type;
static_assert( is_same::value,
"common_type_t uses the right period" );
using C32 = common_type::type;
static_assert( is_same::value,
"common_type_t is common_type_t" );
using C123 = common_type::type;
static_assert( is_same::value,
"common_type of three durations uses the right period" );
using C132 = common_type::type;
static_assert( is_same::value, "order doesn't matter" );
using C312 = common_type::type;
static_assert( is_same::value, "order doesn't matter" );
using C321 = common_type::type;
static_assert( is_same::value, "order doesn't matter" );
using C = common_type>,
duration>>::type;
static_assert( is_same::type,
ratio<1, 6>>>::value, "" );
}
void
test02()
{
using D1 = duration>;
D1 d1;
static_assert( is_same::type>::value,
"unary + returns the reduced duration" );
static_assert( is_same::type>::value,
"unary - returns the reduced duration" );
using D2 = duration>;
D2 d2;
static_assert( is_same::type>::value,
"unary + returns the reduced duration" );
static_assert( is_same::type>::value,
"unary - returns the reduced duration" );
using D3 = duration>;
D3 d3;
static_assert( is_same::type>::value,
"unary + returns the reduced duration" );
static_assert( is_same::type>::value,
"unary - returns the reduced duration" );
}
template
struct Number
{
explicit
Number(T t = 0) : i(t)
{ }
template::value,
typename = typename std::enable_if::type>
explicit
Number(Number n) : i(n.i)
{ }
T i = 0;
Number& operator+=(Number n) { i += n.i; return *this; }
Number& operator-=(Number n) { i -= n.i; return *this; }
Number& operator*=(Number n) { i *= n.i; return *this; }
Number& operator/=(Number n) { i /= n.i; return *this; }
Number& operator%=(Number n) { i %= n.i; return *this; }
Number operator+(Number n) { return Number{ i + n.i }; }
Number operator-(Number n) { return Number{ i - n.i }; }
Number operator*(Number n) { return Number{ i * n.i }; }
Number operator/(Number n) { return Number{ i / n.i }; }
Number operator%(Number n) { return Number{ i % n.i }; }
};
namespace std
{
// Specialise common_type to give a different type
template<>
struct common_type, Number>
{ using type = Number; };
}
void
test03()
{
using D4 = duration, ratio<49, 21>>;
static_assert( is_same::type,
duration, ratio<7, 3>>>::value,
"common_type_t> uses common_type_t" );
D4 d4;
static_assert( is_same::type>::value,
"unary + returns type with common_type_t as rep" );
static_assert( is_same::type>::value,
"unary - returns type with common_type_t as rep" );
}