From 7274deff738213914a31413a9d1a671020694175 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 13 Nov 2009 02:25:12 +0000 Subject: [PATCH] type_traits (declval): Add, per DR 1255. 2009-11-12 Paolo Carlini * include/std/type_traits (declval): Add, per DR 1255. (__is_convertible_helper, common_type): Use it. * include/bits/move.h: Mention std::declval. * testsuite/20_util/declval/requirements/1.cc: New. * testsuite/20_util/declval/requirements/1_neg.cc: Likewise. * testsuite/20_util/common_type/requirements/typedefs-2.cc: Likewise. * testsuite/20_util/common_type/requirements/ explicit_instantiation.cc: Extend. * testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust dg-error line numbers. * testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: Likewise. From-SVN: r154140 --- libstdc++-v3/ChangeLog | 15 ++++ libstdc++-v3/include/bits/move.h | 4 +- libstdc++-v3/include/std/type_traits | 45 ++++++------ .../requirements/explicit_instantiation.cc | 8 ++- .../common_type/requirements/typedefs-2.cc | 72 +++++++++++++++++++ .../20_util/declval/requirements/1.cc | 49 +++++++++++++ .../20_util/declval/requirements/1_neg.cc | 31 ++++++++ .../make_signed/requirements/typedefs_neg.cc | 4 +- .../requirements/typedefs_neg.cc | 4 +- 9 files changed, 205 insertions(+), 27 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-2.cc create mode 100644 libstdc++-v3/testsuite/20_util/declval/requirements/1.cc create mode 100644 libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9357038cf84..4bd07dd7094 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2009-11-12 Paolo Carlini + + * include/std/type_traits (declval): Add, per DR 1255. + (__is_convertible_helper, common_type): Use it. + * include/bits/move.h: Mention std::declval. + * testsuite/20_util/declval/requirements/1.cc: New. + * testsuite/20_util/declval/requirements/1_neg.cc: Likewise. + * testsuite/20_util/common_type/requirements/typedefs-2.cc: Likewise. + * testsuite/20_util/common_type/requirements/ + explicit_instantiation.cc: Extend. + * testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Adjust + dg-error line numbers. + * testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc: + Likewise. + 2009-11-12 Paolo Carlini * include/bits/stl_algobase.h: Include the full , per diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index 668e64c7cd0..91754814d8b 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -35,7 +35,7 @@ #include #ifdef __GXX_EXPERIMENTAL_CXX0X__ -#include +#include // Brings in std::declval too. _GLIBCXX_BEGIN_NAMESPACE(std) @@ -81,6 +81,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) move(_Tp&& __t) { return static_cast::type&&>(__t); } + /// declval, defined in . + _GLIBCXX_END_NAMESPACE #define _GLIBCXX_MOVE(_Tp) std::move(_Tp) diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 788edf66cff..dcfa1c9c4ab 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -240,6 +240,9 @@ namespace std : public integral_constant { }; + template + typename add_rvalue_reference<_Tp>::type declval(); + // Relationships between types. template::value || is_void<_To>::value @@ -255,10 +258,9 @@ namespace std private: static __one __test(_To); static __two __test(...); - static typename add_rvalue_reference<_From>::type __makeFrom(); public: - static const bool __value = sizeof(__test(__makeFrom())) == 1; + static const bool __value = sizeof(__test(declval<_From>())) == 1; }; // XXX FIXME @@ -556,23 +558,11 @@ namespace std template struct common_type<_Tp> - { - static_assert(sizeof(_Tp) > 0, "must be complete type"); - typedef _Tp type; - }; + { typedef _Tp type; }; template - 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(); - - public: - typedef decltype(true ? __t() : __u()) type; - }; + struct common_type<_Tp, _Up> + { typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; }; template struct common_type<_Tp, _Up, _Vp...> @@ -580,11 +570,26 @@ namespace std typedef typename common_type::type, _Vp...>::type type; }; - // @} group metaprogramming + + /// declval + template + struct __declval_protector + { + static const bool __stop = false; + static typename add_rvalue_reference<_Tp>::type __delegate(); + }; + + template + inline typename add_rvalue_reference<_Tp>::type + declval() + { + static_assert(__declval_protector<_Tp>::__stop, + "declval() must not be used!"); + return __declval_protector<_Tp>::__delegate(); + } } #endif // __GXX_EXPERIMENTAL_CXX0X__ -#endif // _GLIBCXX_TYPE_TRAITS - +#endif // _GLIBCXX_TYPE_TRAITS 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 index 6d5ef3066ef..93422ca84ae 100644 --- a/libstdc++-v3/testsuite/20_util/common_type/requirements/explicit_instantiation.cc +++ b/libstdc++-v3/testsuite/20_util/common_type/requirements/explicit_instantiation.cc @@ -18,7 +18,6 @@ // with this library; see the file COPYING3. If not see // . - // NB: This file is for testing type_traits with NO OTHER INCLUDES. #include @@ -29,9 +28,14 @@ namespace std typedef int& test_type2; typedef double test_type3; typedef float test_type4; - + typedef void test_type5; + typedef const void test_type6; + template struct common_type; template struct common_type; template struct common_type; template struct common_type; + + template struct common_type; + template struct common_type; } diff --git a/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-2.cc b/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-2.cc new file mode 100644 index 00000000000..b4a96e5406c --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/common_type/requirements/typedefs-2.cc @@ -0,0 +1,72 @@ +// { dg-options "-std=gnu++0x" } +// 2009-11-12 Paolo Carlini +// +// Copyright (C) 2009 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 +// . + +#include +#include + +// DR 1255. +void test01() +{ + bool test __attribute__((unused)) = true; + using std::common_type; + using std::is_same; + + VERIFY( (is_same::type, void>::value) ); + VERIFY( (is_same::type, const void>::value) ); + VERIFY( (is_same::type, volatile void>::value) ); + VERIFY( (is_same::type, + const volatile void>::value) ); + + VERIFY( (is_same::type, void>::value) ); + VERIFY( (is_same::type, void>::value) ); + VERIFY( (is_same::type, void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + VERIFY( (is_same::type, + void>::value) ); + } + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/20_util/declval/requirements/1.cc b/libstdc++-v3/testsuite/20_util/declval/requirements/1.cc new file mode 100644 index 00000000000..204d4ebb619 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/declval/requirements/1.cc @@ -0,0 +1,49 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } +// 2009-11-12 Paolo Carlini +// +// Copyright (C) 2009 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 +// . + +#include + +template + struct is_convertible_mini + { + private: + typedef char one; + typedef struct { char arr[2]; } two; + + static one test(To); + static two test(...); + + public: + static const bool value = sizeof(test(std::declval())) == 1; +}; + +template + const bool is_convertible_mini::value; + +void test01() +{ + static_assert(is_convertible_mini::value, "#1"); + static_assert(!is_convertible_mini::value, "#2"); + static_assert(is_convertible_mini::value, "#3"); + static_assert(!is_convertible_mini::value, "#4"); + static_assert(is_convertible_mini::value, "#5"); + static_assert(!is_convertible_mini::value, "#6"); +} diff --git a/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc new file mode 100644 index 00000000000..d22a58ba038 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc @@ -0,0 +1,31 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } +// 2009-11-12 Paolo Carlini +// +// Copyright (C) 2009 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-error "static assertion failed" "" { target *-*-* } 587 } +// { dg-error "instantiated from here" "" { target *-*-* } 30 } +// { dg-excess-errors "In function" } + +#include + +void test01() +{ + std::declval(); +} diff --git a/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc index 2ae375454e0..65fc2cece7c 100644 --- a/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc +++ b/libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc @@ -48,8 +48,8 @@ void test01() // { dg-error "instantiated from here" "" { target *-*-* } 40 } // { dg-error "instantiated from here" "" { target *-*-* } 42 } -// { dg-error "invalid use of incomplete type" "" { target *-*-* } 547 } -// { dg-error "declaration of" "" { target *-*-* } 509 } +// { dg-error "invalid use of incomplete type" "" { target *-*-* } 549 } +// { dg-error "declaration of" "" { target *-*-* } 511 } // { dg-excess-errors "At global scope" } // { dg-excess-errors "In instantiation of" } diff --git a/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc index 5bf030cabcb..795755f7854 100644 --- a/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc +++ b/libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc @@ -48,8 +48,8 @@ void test01() // { dg-error "instantiated from here" "" { target *-*-* } 40 } // { dg-error "instantiated from here" "" { target *-*-* } 42 } -// { dg-error "invalid use of incomplete type" "" { target *-*-* } 468 } -// { dg-error "declaration of" "" { target *-*-* } 430 } +// { dg-error "invalid use of incomplete type" "" { target *-*-* } 470 } +// { dg-error "declaration of" "" { target *-*-* } 432 } // { dg-excess-errors "At global scope" } // { dg-excess-errors "In instantiation of" }