Commit 83ddb39f by Daniel Krugler Committed by Paolo Carlini

type_traits (result_of): Provide "SFINAE-friendly" (see N3436) implementation.

2012-10-01  Daniel Krugler  <daniel.kruegler@googlemail.com>

	* include/std/type_traits (result_of): Provide "SFINAE-friendly"
	(see N3436) implementation.
	* testsuite/20_util/result_of/sfinae_friendly_1.cc: New.
	* testsuite/20_util/result_of/sfinae_friendly_2.cc: Likewise.
	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Tweak
	dg-error line numbers.
	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
	Likewise.
	* testsuite/20_util/declval/requirements/1_neg.cc: Likewise.

From-SVN: r191930
parent cd8b6dc5
2012-10-01 Daniel Krugler <daniel.kruegler@googlemail.com>
* include/std/type_traits (result_of): Provide "SFINAE-friendly"
(see N3436) implementation.
* testsuite/20_util/result_of/sfinae_friendly_1.cc: New.
* testsuite/20_util/result_of/sfinae_friendly_2.cc: Likewise.
* testsuite/20_util/make_signed/requirements/typedefs_neg.cc: Tweak
dg-error line numbers.
* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
Likewise.
* testsuite/20_util/declval/requirements/1_neg.cc: Likewise.
2012-09-30 Benjamin Kosnik <bkoz@redhat.com> 2012-09-30 Benjamin Kosnik <bkoz@redhat.com>
* doc/doxygen/user.cfg.in: Update to doxygen 1.8.2. * doc/doxygen/user.cfg.in: Update to doxygen 1.8.2.
......
// C++11 type_traits -*- C++ -*- // C++11 type_traits -*- C++ -*-
// Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 // Copyright (C) 2007-2012 Free Software Foundation, Inc.
// Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -1819,89 +1818,171 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1819,89 +1818,171 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Signature> template<typename _Signature>
class result_of; class result_of;
// sfinae-friendly result_of implementation. We use enable_if to transport
// both the result information (as the member type) and the failure
// information (no member type).
template<typename _Tp>
using __success_type = enable_if<true, _Tp>;
using __failure_type = enable_if<false>;
// [func.require] paragraph 1 bullet 1:
struct __result_of_memfun_ref_impl
{
template<typename _Fp, typename _Tp1, typename... _Args>
static __success_type<decltype(
(std::declval<_Tp1>().*std::declval<_Fp>())(std::declval<_Args>()...)
)> _S_test(int);
template<typename...>
static __failure_type _S_test(...);
};
template<typename _MemPtr, typename _Arg, typename... _Args>
struct __result_of_memfun_ref
: private __result_of_memfun_ref_impl
{
typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type;
};
// [func.require] paragraph 1 bullet 2:
struct __result_of_memfun_deref_impl
{
template<typename _Fp, typename _Tp1, typename... _Args>
static __success_type<decltype(
((*std::declval<_Tp1>()).*std::declval<_Fp>())(std::declval<_Args>()...)
)> _S_test(int);
template<typename...>
static __failure_type _S_test(...);
};
template<typename _MemPtr, typename _Arg, typename... _Args>
struct __result_of_memfun_deref
: private __result_of_memfun_deref_impl
{
typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type;
};
// [func.require] paragraph 1 bullet 3:
struct __result_of_memobj_ref_impl
{
template<typename _Fp, typename _Tp1>
static __success_type<decltype(
std::declval<_Tp1>().*std::declval<_Fp>()
)> _S_test(int);
template<typename, typename>
static __failure_type _S_test(...);
};
template<typename _MemPtr, typename _Arg>
struct __result_of_memobj_ref
: private __result_of_memobj_ref_impl
{
typedef decltype(_S_test<_MemPtr, _Arg>(0)) type;
};
// [func.require] paragraph 1 bullet 4:
struct __result_of_memobj_deref_impl
{
template<typename _Fp, typename _Tp1>
static __success_type<decltype(
(*std::declval<_Tp1>()).*std::declval<_Fp>()
)> _S_test(int);
template<typename, typename>
static __failure_type _S_test(...);
};
template<typename _MemPtr, typename _Arg> template<typename _MemPtr, typename _Arg>
struct _Result_of_memobj; struct __result_of_memobj_deref
: private __result_of_memobj_deref_impl
{
typedef decltype(_S_test<_MemPtr, _Arg>(0)) type;
};
template<typename _MemPtr, typename _Arg>
struct __result_of_memobj;
template<typename _Res, typename _Class, typename _Arg> template<typename _Res, typename _Class, typename _Arg>
struct _Result_of_memobj<_Res _Class::*, _Arg> struct __result_of_memobj<_Res _Class::*, _Arg>
{ {
private: typedef typename remove_cv<typename remove_reference<
typedef _Res _Class::* _Func; _Arg>::type>::type _Argval;
typedef _Res _Class::* _MemPtr;
template<typename _Tp> typedef typename conditional<__or_<is_same<_Argval, _Class>,
static _Tp _S_get(const _Class&); is_base_of<_Class, _Argval>>::value,
template<typename _Tp> __result_of_memobj_ref<_MemPtr, _Arg>,
static _Tp _S_get(const volatile _Class&); __result_of_memobj_deref<_MemPtr, _Arg>
template<typename _Tp> >::type::type type;
static decltype(*std::declval<_Tp>()) _S_get(...);
public:
typedef
decltype(_S_get<_Arg>(std::declval<_Arg>()).*std::declval<_Func>())
__type;
}; };
template<typename _MemPtr, typename _Arg, typename... _ArgTypes> template<typename _MemPtr, typename _Arg, typename... _Args>
struct _Result_of_memfun; struct __result_of_memfun;
template<typename _Res, typename _Class, typename _Arg, typename... _Args> template<typename _Res, typename _Class, typename _Arg, typename... _Args>
struct _Result_of_memfun<_Res _Class::*, _Arg, _Args...> struct __result_of_memfun<_Res _Class::*, _Arg, _Args...>
{ {
private: typedef typename remove_cv<typename remove_reference<
typedef _Res _Class::* _Func; _Arg>::type>::type _Argval;
typedef _Res _Class::* _MemPtr;
template<typename _Tp> typedef typename conditional<__or_<is_same<_Argval, _Class>,
static _Tp _S_get(const _Class&); is_base_of<_Class, _Argval>>::value,
template<typename _Tp> __result_of_memfun_ref<_MemPtr, _Arg, _Args...>,
static _Tp _S_get(const volatile _Class&); __result_of_memfun_deref<_MemPtr, _Arg, _Args...>
template<typename _Tp> >::type::type type;
static decltype(*std::declval<_Tp>()) _S_get(...);
public:
typedef
decltype((_S_get<_Arg>(std::declval<_Arg>()).*std::declval<_Func>())
(std::declval<_Args>()...) )
__type;
}; };
template<bool, bool, typename _Functor, typename... _ArgTypes> template<bool, bool, typename _Functor, typename... _ArgTypes>
struct _Result_of_impl; struct __result_of_impl
template<typename _Functor, typename... _ArgTypes>
struct _Result_of_impl<false, false, _Functor, _ArgTypes...>
{ {
typedef typedef __failure_type type;
decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
__type;
}; };
template<typename _MemPtr, typename _Arg> template<typename _MemPtr, typename _Arg>
struct _Result_of_impl<true, false, _MemPtr, _Arg> struct __result_of_impl<true, false, _MemPtr, _Arg>
: _Result_of_memobj<typename decay<_MemPtr>::type, _Arg> : public __result_of_memobj<typename decay<_MemPtr>::type, _Arg>
{ }; { };
template<typename _MemPtr, typename _Arg, typename... _ArgTypes> template<typename _MemPtr, typename _Arg, typename... _Args>
struct _Result_of_impl<false, true, _MemPtr, _Arg, _ArgTypes...> struct __result_of_impl<false, true, _MemPtr, _Arg, _Args...>
: _Result_of_memfun<typename decay<_MemPtr>::type, _Arg, _ArgTypes...> : public __result_of_memfun<typename decay<_MemPtr>::type, _Arg, _Args...>
{ }; { };
// [func.require] paragraph 1 bullet 5:
struct __result_of_other_impl
{
template<typename _Fn, typename... _Args>
static __success_type<decltype(
std::declval<_Fn>()(std::declval<_Args>()...)
)> _S_test(int);
template<typename...>
static __failure_type _S_test(...);
};
template<typename _Functor, typename... _ArgTypes> template<typename _Functor, typename... _ArgTypes>
struct result_of<_Functor(_ArgTypes...)> struct __result_of_impl<false, false, _Functor, _ArgTypes...>
: _Result_of_impl<is_member_object_pointer< : private __result_of_other_impl
typename remove_reference<_Functor>::type >::value,
is_member_function_pointer<
typename remove_reference<_Functor>::type >::value,
_Functor, _ArgTypes...>
{ {
typedef typename _Result_of_impl< typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type;
is_member_object_pointer<
typename remove_reference<_Functor>::type >::value,
is_member_function_pointer<
typename remove_reference<_Functor>::type >::value,
_Functor, _ArgTypes...>::__type
type;
}; };
template<typename _Functor, typename... _ArgTypes>
struct result_of<_Functor(_ArgTypes...)>
: public __result_of_impl<
is_member_object_pointer<
typename remove_reference<_Functor>::type
>::value,
is_member_function_pointer<
typename remove_reference<_Functor>::type
>::value,
_Functor, _ArgTypes...
>::type
{ };
/** /**
* Use SFINAE to determine if the type _Tp has a publicly-accessible * Use SFINAE to determine if the type _Tp has a publicly-accessible
* member type _NTYPE. * member type _NTYPE.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// { dg-do compile } // { dg-do compile }
// 2009-11-12 Paolo Carlini <paolo.carlini@oracle.com> // 2009-11-12 Paolo Carlini <paolo.carlini@oracle.com>
// //
// Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc. // Copyright (C) 2009-2012 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
// with this library; see the file COPYING3. If not see // with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
// { dg-error "static assertion failed" "" { target *-*-* } 1813 } // { dg-error "static assertion failed" "" { target *-*-* } 1812 }
#include <utility> #include <utility>
......
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
// 2007-05-03 Benjamin Kosnik <bkoz@redhat.com> // 2007-05-03 Benjamin Kosnik <bkoz@redhat.com>
// //
// Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 // Copyright (C) 2007-2012 Free Software Foundation, Inc.
// Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -43,11 +42,11 @@ void test01() ...@@ -43,11 +42,11 @@ void test01()
typedef make_signed<float>::type test5_type; typedef make_signed<float>::type test5_type;
} }
// { dg-error "does not name a type" "" { target *-*-* } 34 } // { dg-error "does not name a type" "" { target *-*-* } 33 }
// { dg-error "required from here" "" { target *-*-* } 36 } // { dg-error "required from here" "" { target *-*-* } 35 }
// { dg-error "required from here" "" { target *-*-* } 38 } // { dg-error "required from here" "" { target *-*-* } 37 }
// { dg-error "required from here" "" { target *-*-* } 41 } // { dg-error "required from here" "" { target *-*-* } 40 }
// { dg-error "required from here" "" { target *-*-* } 43 } // { dg-error "required from here" "" { target *-*-* } 42 }
// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1602 } // { dg-error "invalid use of incomplete type" "" { target *-*-* } 1601 }
// { dg-error "declaration of" "" { target *-*-* } 1566 } // { dg-error "declaration of" "" { target *-*-* } 1565 }
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
// 2007-05-03 Benjamin Kosnik <bkoz@redhat.com> // 2007-05-03 Benjamin Kosnik <bkoz@redhat.com>
// //
// Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 // Copyright (C) 2007-2012 Free Software Foundation, Inc.
// Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -43,11 +42,11 @@ void test01() ...@@ -43,11 +42,11 @@ void test01()
typedef make_unsigned<float>::type test5_type; typedef make_unsigned<float>::type test5_type;
} }
// { dg-error "does not name a type" "" { target *-*-* } 34 } // { dg-error "does not name a type" "" { target *-*-* } 33 }
// { dg-error "required from here" "" { target *-*-* } 36 } // { dg-error "required from here" "" { target *-*-* } 35 }
// { dg-error "required from here" "" { target *-*-* } 38 } // { dg-error "required from here" "" { target *-*-* } 37 }
// { dg-error "required from here" "" { target *-*-* } 41 } // { dg-error "required from here" "" { target *-*-* } 40 }
// { dg-error "required from here" "" { target *-*-* } 43 } // { dg-error "required from here" "" { target *-*-* } 42 }
// { dg-error "invalid use of incomplete type" "" { target *-*-* } 1520 } // { dg-error "invalid use of incomplete type" "" { target *-*-* } 1519 }
// { dg-error "declaration of" "" { target *-*-* } 1484 } // { dg-error "declaration of" "" { target *-*-* } 1483 }
// { dg-options "-std=gnu++11" }
// { dg-do compile }
// Copyright (C) 2012 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
// <http://www.gnu.org/licenses/>.
// Taken from N3436:
#include <type_traits>
#include <string>
struct eat { template<class T> eat(T const &) {} };
struct not_incrementable {};
struct inc {
template<class T>
auto operator()(T t) const -> decltype(t++)
{ return t++; }
};
template<class A>
typename std::result_of<inc(A)>::type // sfinae here
try_inc(A a) {
return inc()(a);
}
not_incrementable
try_inc(eat) {
return not_incrementable();
}
template<class>
struct never { static const bool value = false; };
template<class T>
struct Fail
{
static_assert(never<T>::value, "duh");
typedef int type;
};
struct Fun
{
template<class T>
typename Fail<T>::type operator()(T)
{ return 0; }
};
template<class T>
typename std::result_of<Fun(T)>::type foo(T)
{ return 0; }
template<class>
int foo(...)
{ return 0; }
void result_of_sfinae() {
int x = try_inc(1); // OK
not_incrementable y = try_inc(std::string("foo")); // OK, not_incrementable
(void) x;
(void) y;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment