mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
boost/utility/result_of.hpp, boost/utility/detail/result_of_iterate.hpp:
- result_of implementation libs/utility/test/result_of_test.cpp: - result_of tests libs/utility/test/Jamfile, libs/utility/test/Jamfile.v2: - run result_of tests libs/utility/utility.htm: - document result_of libs/libraries.htm: - list result_of index.htm: - announce result_of [SVN r22720]
This commit is contained in:
parent
b6c826a139
commit
491db15997
78
include/boost/utility/detail/result_of_iterate.hpp
Normal file
78
include/boost/utility/detail/result_of_iterate.hpp
Normal file
@ -0,0 +1,78 @@
|
||||
#if !defined(BOOST_PP_IS_ITERATING)
|
||||
# error Boost result_of - do not include this file!
|
||||
#endif
|
||||
|
||||
// CWPro8 requires an argument in a function type specialization
|
||||
#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3002)) && BOOST_PP_ITERATION() == 0
|
||||
# define BOOST_RESULT_OF_ARGS void
|
||||
#else
|
||||
# define BOOST_RESULT_OF_ARGS BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),T)
|
||||
#endif
|
||||
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
||||
template<typename F BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<F(BOOST_RESULT_OF_ARGS)>
|
||||
: detail::result_of<F, F(BOOST_RESULT_OF_ARGS)> {};
|
||||
#endif
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (*)(BOOST_RESULT_OF_ARGS), FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (&)(BOOST_RESULT_OF_ARGS), FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
#undef BOOST_RESULT_OF_ARGS
|
||||
|
||||
#if BOOST_PP_ITERATION() > 1 && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (T0::*)
|
||||
(BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T)),
|
||||
FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (T0::*)
|
||||
(BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
|
||||
const,
|
||||
FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (T0::*)
|
||||
(BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
|
||||
volatile,
|
||||
FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
|
||||
template<typename R, typename FArgs BOOST_PP_COMMA_IF(BOOST_PP_ITERATION())
|
||||
BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
|
||||
struct result_of<R (T0::*)
|
||||
(BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
|
||||
const volatile,
|
||||
FArgs>
|
||||
{
|
||||
typedef R type;
|
||||
};
|
||||
#endif
|
||||
|
||||
}
|
63
include/boost/utility/result_of.hpp
Normal file
63
include/boost/utility/result_of.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
// Boost result_of library
|
||||
|
||||
// Copyright Doug Gregor 2004. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// For more information, see http://www.boost.org/libs/utility
|
||||
#ifndef BOOST_RESULT_OF_HPP
|
||||
#define BOOST_RESULT_OF_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/type_traits/ice.hpp>
|
||||
#include <boost/type.hpp>
|
||||
#include <boost/preprocessor.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/mpl/aux_/has_xxx.hpp>
|
||||
|
||||
#ifndef BOOST_RESULT_OF_NUM_ARGS
|
||||
# define BOOST_RESULT_OF_NUM_ARGS 10
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
template<typename F> struct result_of;
|
||||
|
||||
#if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
namespace detail {
|
||||
|
||||
BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
|
||||
|
||||
template<typename F, typename FArgs, bool HasResultType> struct get_result_of;
|
||||
|
||||
template<typename F, typename FArgs>
|
||||
struct get_result_of<F, FArgs, true>
|
||||
{
|
||||
typedef typename F::result_type type;
|
||||
};
|
||||
|
||||
template<typename F, typename FArgs>
|
||||
struct get_result_of<F, FArgs, false>
|
||||
{
|
||||
typedef typename F::template result<FArgs>::type type;
|
||||
};
|
||||
|
||||
template<typename F>
|
||||
struct get_result_of<F, F(void), false>
|
||||
{
|
||||
typedef void type;
|
||||
};
|
||||
|
||||
template<typename F, typename FArgs>
|
||||
struct result_of : get_result_of<F, FArgs, (has_result_type<F>::value)> {};
|
||||
|
||||
} // end namespace detail
|
||||
|
||||
#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,BOOST_RESULT_OF_NUM_ARGS,<boost/utility/detail/result_of_iterate.hpp>))
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
}
|
||||
#endif // can't use result_of
|
||||
|
||||
#endif // BOOST_RESULT_OF_HPP
|
@ -40,4 +40,5 @@ test-suite utility
|
||||
[ run ../enable_if_no_disambiguation.cpp $(test_monitor) ]
|
||||
[ run ../enable_if_partial_specializations.cpp $(test_monitor) ]
|
||||
[ run next_prior_test.cpp $(test_monitor) ]
|
||||
[ compile result_of_test.cpp ]
|
||||
;
|
||||
|
@ -30,4 +30,5 @@ test-suite utility
|
||||
[ run ../enable_if_namespace_disambiguation.cpp ../../test/build//boost_test_exec_monitor ]
|
||||
[ run ../enable_if_no_disambiguation.cpp ../../test/build//boost_test_exec_monitor ]
|
||||
[ run ../enable_if_partial_specializations.cpp ../../test/build//boost_test_exec_monitor ]
|
||||
[ compile result_of_test.cpp ]
|
||||
;
|
||||
|
45
test/result_of_test.cpp
Normal file
45
test/result_of_test.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include <boost/utility/result_of.hpp>
|
||||
#include <utility>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
|
||||
struct int_result_type { typedef int result_type; };
|
||||
|
||||
struct int_result_of
|
||||
{
|
||||
template<typename F> struct result { typedef int type; };
|
||||
};
|
||||
|
||||
struct int_result_type_and_float_result_of
|
||||
{
|
||||
typedef int result_type;
|
||||
template<typename F> struct result { typedef float type; };
|
||||
};
|
||||
|
||||
struct X {};
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost;
|
||||
|
||||
typedef int (*func_ptr)(float, double);
|
||||
typedef int (&func_ref)(float, double);
|
||||
typedef int (X::*mem_func_ptr)(float);
|
||||
typedef int (X::*mem_func_ptr_c)(float) const;
|
||||
typedef int (X::*mem_func_ptr_v)(float) volatile;
|
||||
typedef int (X::*mem_func_ptr_cv)(float) const volatile;
|
||||
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<int_result_type(float)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<int_result_of(double)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<int_result_of(void)>::type, void>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<const int_result_of(double)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<volatile int_result_of(void)>::type, void>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<int_result_type_and_float_result_of(char)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<func_ptr(char, float)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<func_ref(char, float)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<mem_func_ptr(X,char)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<mem_func_ptr_c(X,char)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<mem_func_ptr_v(X,char)>::type, int>::value));
|
||||
BOOST_STATIC_ASSERT((is_same<result_of<mem_func_ptr_cv(X,char)>::type, int>::value));
|
||||
return 0;
|
||||
}
|
43
utility.htm
43
utility.htm
@ -22,6 +22,7 @@
|
||||
Class <a href="#Class_noncopyable">noncopyable</a></li>
|
||||
<li>
|
||||
Function template <a href="#addressof">addressof()</a></li>
|
||||
<li>Class template <a href="#result_of">result_of</a></li>
|
||||
</ul>
|
||||
<h2>
|
||||
Function templates <a name="checked_delete">checked_delete</a>() and
|
||||
@ -136,11 +137,51 @@ void f() {
|
||||
// nonaddressable* xpe = &x; /* error */
|
||||
}</pre>
|
||||
</blockquote>
|
||||
<h2><a name="result_of">Class template
|
||||
result_of</a></h2> <p>The class template
|
||||
<code>result_of</code> helps determine the type of a
|
||||
call expression. Given an lvalue <code>f</code> of
|
||||
type <code>F</code> and lvalues <code>t1</code>,
|
||||
<code>t2</code>, ..., <code>t<em>N</em></code> of
|
||||
types <code>T1</code>, <code>T2</code>, ...,
|
||||
<code>T<em>N</em></code>, respectively, the type
|
||||
<code>result_of<F(T1, T2, ...,
|
||||
T<em>N</em>)>::type</code> defines the result type
|
||||
of the expression <code>f(t1, t2,
|
||||
...,t<em>N</em>)</code>. The implementation permits
|
||||
the type <code>F</code> to be a function pointer,
|
||||
function reference, member function pointer, or class
|
||||
type. When <code>F</code> is a class type with a
|
||||
member type <code>result_type</code>,
|
||||
<code>result_of<F(T1, T2, ...,
|
||||
T<em>N</em>)></code> is
|
||||
<code>F::result_type</code>. Otherwise,
|
||||
<code>result_of<F(T1, T2, ...,
|
||||
T<em>N</em>)></code> is <code>F::result<F(T1,
|
||||
T2, ..., T<em>N</em>)>::type</code> when
|
||||
<code><em>N</em> > 0</code> or <code>void</code>
|
||||
when <code><em>N</em> = 0</code>. For additional
|
||||
information about <code>result_of</code>, see the
|
||||
current draft of the C++ Library TR, <a
|
||||
href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2004/n1647.pdf">N1647</a>,
|
||||
or the <code>result_of</code> <a
|
||||
href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1454.html">proposal</a>.</p>
|
||||
|
||||
<p>Class template <code>result_of</code> resides in
|
||||
the header <code><<a
|
||||
href="../../boost/utility/result_of.hpp">boost/utility/result_of.hpp</a>></code>. By
|
||||
default, <em>N</em> may be any value between 0 and
|
||||
10. To change the upper limit, define the macro
|
||||
<code>BOOST_RESULT_OF_NUM_ARGS</code> to the maximum
|
||||
value for <em>N</em>.</p>
|
||||
|
||||
<p>This implementation of <code>result_of</code> requires class template partial specialization, the ability to parse function types properly, and support for SFINAE. Contributed by Doug Gregor.</p>
|
||||
|
||||
<h2>Class templates for the Base-from-Member Idiom</h2>
|
||||
<p>See <a href="base_from_member.html">separate documentation</a>.</p>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
|
||||
-->23 December, 2003<!--webbot bot="Timestamp" endspan i-checksum="38582"
|
||||
-->02 May, 2004<!--webbot bot="Timestamp" endspan i-checksum="38582"
|
||||
-->
|
||||
</p>
|
||||
<p>© Copyright boost.org 1999-2003. Permission to copy, use, modify, sell and distribute
|
||||
|
Loading…
x
Reference in New Issue
Block a user