From 2637dfcc592c0a1e985ac889e1e589fd469801d2 Mon Sep 17 00:00:00 2001
From: Eric Niebler <boost/utility/result_of.hpp>
.
If your compiler supports decltype
,
- then you can enable automatic result type deduction by
- defining the macro BOOST_RESULT_OF_USE_DECLTYPE
,
- as in the following example.
If your compiler's support for decltype
is
+ adequate, result_of
automatically uses it to
+ deduce the result type of your callable.
--#define BOOST_RESULT_OF_USE_DECLTYPE -#include <boost/utility/result_of.hpp> +#include <boost/utility/result_of.hpp> struct functor { template<class T> @@ -180,21 +178,29 @@ struct functor { typedef boost::result_of< functor(int) ->::type type;+>::type type; // type is int
If decltype
is not enabled,
+
You can test whether result_of
is using
+ decltype
checking if the macro
+ BOOST_RESULT_OF_USE_DECLTYPE
is defined after
+ including result_of.hpp
. You can also force
+ result_of
to use decltype
by
+ defining BOOST_RESULT_OF_USE_DECLTYPE
prior
+ to including result_of.hpp
.
If decltype
is not used,
then automatic result type deduction of function
objects is not possible. Instead, result_of
uses the following protocol to allow the programmer to
specify a type. When F
is a class type with a
member type result_type
,
result_of<F(T1, T2, ...,
- TN)>
is
+ TN)>::type is
F::result_type
. When F
does
not contain result_type
,
result_of<F(T1, T2, ...,
- TN)>
is F::result<F(T1,
+ TN)>::type
is F::result<F(T1,
T2, ..., TN)>::type
when
N > 0
or void
when N = 0
. Note that it is the
@@ -221,22 +227,29 @@ typedef boost::result_of<
typedef boost::result_of<
functor(int)
->::type type;
+>::type type; // type is int
-
In a future
- release, BOOST_RESULT_OF_USE_DECLTYPE
- may be enabled by default on compilers that
- support decltype
, so if you use the above
- protocol please take care to ensure that
- the result_type
- and result<>
members accurately
- represent the result type. If you wish to continue to
+
If you are writing a reusable function object
+ that should work with result_of
, for
+ maximum portability, you might consider following
+ the above protocol even if your compiler has
+ proper decltype
support. If you do,
+ take care to ensure that the
+ result_type
and
+ result<>
members accurately
+ represent the return type of
+ operator()
.
If you wish to continue to
use the protocol on compilers that
support decltype
,
use boost::tr1_result_of
, which is also
defined
- in <boost/utility/result_of.hpp>
.
<boost/utility/result_of.hpp>
. You can also define the macro
+ BOOST_RESULT_OF_USE_TR1
, which causes
+ result_of
to use the convention described
+ above instead of decltype
.
This implementation of result_of