diff options
author | Chris Fairles <chris.fairles@gmail.com> | 2008-07-08 11:59:22 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2008-07-08 11:59:22 +0000 |
commit | cfa9a96b646cc033dcda6a6303e0f702d4d44139 (patch) | |
tree | ff71a5c32cd79d817b37d451e0f45a0794d58ab8 | |
parent | 5acba8a21a2fc1b9de73843bc59ff39d58a81a07 (diff) | |
download | gcc-cfa9a96b646cc033dcda6a6303e0f702d4d44139.zip gcc-cfa9a96b646cc033dcda6a6303e0f702d4d44139.tar.gz gcc-cfa9a96b646cc033dcda6a6303e0f702d4d44139.tar.bz2 |
type_traits: Add common_type.
2008-07-08 Chris Fairles <chris.fairles@gmail.com>
* include/std/type_traits: Add common_type.
* testsuite/20_util/common_type/requirements/
explicit_instantiation.cc: New.
* testsuite/20_util/common_type/requirements/
typedefs-1.cc: Likewise.
From-SVN: r137618
4 files changed, 207 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d4c02c7..da30a6a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2008-07-08 Chris Fairles <chris.fairles@gmail.com> + + * include/std/type_traits: Add common_type. + * testsuite/20_util/common_type/requirements/ + explicit_instantiation.cc: New. + * testsuite/20_util/common_type/requirements/ + typedefs-1.cc: Likewise. + 2008-07-07 Paolo Carlini <paolo.carlini@oracle.com> * testsuite/lib/libstdc++.exp (check_v3_target_stdint): New. diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 6c97280..1a75918 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -551,6 +551,41 @@ namespace std // Integral, but don't define. template<> struct make_signed<bool>; + + template<typename... _Tp> + struct common_type; + + template<typename _Tp> + struct common_type<_Tp> + { + static_assert(sizeof(_Tp) > 0, "must be complete type"); + typedef _Tp type; + }; + + template<typename _Tp, typename _Up> + class common_type<_Tp, _Up> + { + static_assert(sizeof(_Tp) > 0, "must be complete type"); + static_assert(sizeof(_Up) > 0, "must be complete type"); + + static _Tp&& __t(); + static _Up&& __u(); + + // HACK: Prevents optimization of ?: in the decltype + // expression when the condition is the literal, "true". + // See, PR36628. + static bool __true_or_false(); + + public: + typedef decltype(__true_or_false() ? __t() : __u()) type; + }; + + template<typename _Tp, typename _Up, typename... _Vp> + struct common_type<_Tp, _Up, _Vp...> + { + typedef typename + common_type<typename common_type<_Tp, _Up>::type, _Vp...>::type type; + }; } #endif // __GXX_EXPERIMENTAL_CXX0X__ diff --git a/libstdc++-v3/testsuite/20_util/common_type/requirements/explicit_instantiation.cc b/libstdc++-v3/testsuite/20_util/common_type/requirements/explicit_instantiation.cc new file mode 100644 index 0000000..fcc5b2a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/common_type/requirements/explicit_instantiation.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } + +// Copyright (C) 2008 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +// NB: This file is for testing type_traits with NO OTHER INCLUDES. + +#include <type_traits> + +namespace std +{ + typedef int test_type1; + typedef int& test_type2; + typedef double test_type3; + typedef float test_type4; + + template struct common_type<test_type1>; + template struct common_type<test_type1, test_type2>; + template struct common_type<test_type1, test_type2, test_type3>; + template struct common_type<test_type1, test_type2, test_type3, test_type4>; +} diff --git a/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-1.cc b/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-1.cc new file mode 100644 index 0000000..e4152e5 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-1.cc @@ -0,0 +1,118 @@ +// { dg-options "-std=gnu++0x" } +// +// Copyright (C) 2008 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include <type_traits> +#include <testsuite_hooks.h> + +#define JOIN( X, Y ) DO_JOIN( X, Y ) +#define DO_JOIN( X, Y ) DO_JOIN2(X,Y) +#define DO_JOIN2( X, Y ) X##Y + +#define COMMON_TYPE_TEST_1(type1, uid) \ + typedef common_type<type1>::type JOIN(test_t,uid); \ + VERIFY( (is_same<JOIN(test_t,uid), JOIN(test_t,uid)>::value) ); \ + typedef common_type<const type1>::type JOIN(test_t,JOIN(uid,c)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,c)), \ + JOIN(test_t,JOIN(uid,c))>::value) ); \ + typedef common_type<volatile type1>::type JOIN(test_t,JOIN(uid,v)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,v)), \ + JOIN(test_t,JOIN(uid,v))>::value) ); \ + typedef common_type<const volatile type1>::type JOIN(test_t,JOIN(uid,cv)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,cv)), \ + JOIN(test_t,JOIN(uid,cv))>::value) ); \ + typedef common_type<type1 &>::type JOIN(test_t,JOIN(uid,l)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,l)), \ + JOIN(test_t,JOIN(uid,l))>::value) ); \ + typedef common_type<const type1 &>::type JOIN(test_t,JOIN(uid,lc)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,lc)), \ + JOIN(test_t,JOIN(uid,lc))>::value) ); \ + typedef common_type<volatile type1 &>::type JOIN(test_t,JOIN(uid,lv)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,lv)), \ + JOIN(test_t,JOIN(uid,lv))>::value) ); \ + typedef common_type<const volatile type1 &>::type JOIN(test_t,JOIN(uid,lcv)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,lcv)), \ + JOIN(test_t,JOIN(uid,lcv))>::value) ); \ + typedef common_type<type1 &&>::type JOIN(test_t,JOIN(uid,r)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,r)), \ + JOIN(test_t,JOIN(uid,r))>::value) ); \ + typedef common_type<const type1 &&>::type JOIN(test_t,JOIN(uid,rc)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,rc)), \ + JOIN(test_t,JOIN(uid,rc))>::value) ); \ + typedef common_type<volatile type1 &&>::type JOIN(test_t,JOIN(uid,rv)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,rv)), \ + JOIN(test_t,JOIN(uid,rv))>::value) ); \ + typedef common_type<const volatile type1 &&>::type JOIN(test_t,JOIN(uid,rcv)); \ + VERIFY( (is_same<JOIN(test_t,JOIN(uid,rcv)), \ + JOIN(test_t,JOIN(uid,rcv))>::value) ) + +struct A { }; +struct B : A { }; + +void test01() +{ + bool test __attribute__((unused)) = true; + using std::common_type; + using std::is_same; + + // Positive tests. + COMMON_TYPE_TEST_1(int, 1); + COMMON_TYPE_TEST_1(double, 2); + COMMON_TYPE_TEST_1(A, 3); + COMMON_TYPE_TEST_1(B, 4); +} + +#define COMMON_TYPE_TEST_2_IMPL(type1, type2, type3, uid) \ + typedef common_type<type1, type2>::type JOIN(JOIN(test, uid),_t1); \ + typedef common_type<type2, type1>::type JOIN(JOIN(test, uid),_t2); \ + VERIFY( (is_same<JOIN(JOIN(test, uid),_t1), type3>::value) ); \ + VERIFY( (is_same<JOIN(JOIN(test, uid),_t2), type3>::value) ) + +#define NO_CV + +#define COMMON_TYPE_TEST_2(cv_qual, type1, type2, type3, uid) \ + COMMON_TYPE_TEST_2_IMPL(cv_qual type1, type2, type3, uid); \ + COMMON_TYPE_TEST_2_IMPL(cv_qual type1 &, type2, type3, JOIN(uid,l)); \ + COMMON_TYPE_TEST_2_IMPL(cv_qual type1 &&, type2, type3, JOIN(uid,r)) + +#define COMMON_TYPE_TEST_ALL_2(type1, type2, type3, uid) \ + COMMON_TYPE_TEST_2(NO_CV, type1, type2, type3, uid); \ + COMMON_TYPE_TEST_2(const, type1, type2, type3, uid); \ + COMMON_TYPE_TEST_2(volatile, type1, type2, type3, uid); \ + COMMON_TYPE_TEST_2(const volatile, type1, type2, type3, uid) + +void test02() +{ + bool test __attribute__((unused)) = true; + using std::common_type; + using std::is_same; + + COMMON_TYPE_TEST_ALL_2(int, int, int, 1); + COMMON_TYPE_TEST_ALL_2(int, double, double, 2); + COMMON_TYPE_TEST_2(NO_CV, A, A, A, 3); + COMMON_TYPE_TEST_2(const, A, A, const A, 4); + COMMON_TYPE_TEST_2(NO_CV, B, A, A, 5); +} + +int main() +{ + test01(); + test02(); + return 0; +} |