Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-01-07 00:31:10


The following trivial use of boost::result_of fails on msvc-7.1 and
msvc-8.0:

     #include <boost/utility/result_of.hpp>

     template<typename T>
     struct function
     {
         template<typename Sig>
         struct result;

         template<typename This, typename U>
         struct result<This(U)>
         {
             typedef U type;
         };
     };

     int main()
     {
         boost::result_of<function<void>(int)>::type i = 0;
         return 0;
     }

The attached patch works around the problem. (Yes, I've reported this to
Microsoft.) I've also added some additional test cases to catch this
problem (see result_of_test.cpp attached).

I've confirmed the tests pass on msvc-7.1, msvc-8.0, gcc-3.4, gcc-4.0
and gcc-4.1. The following compilers should also be tested, but I don't
have access to them. Can anybody help out?

gcc-3.3
cw-9.4
intel-*
sun-5.8
qcc-*
hp_cxx_*
acc

Any help is much appreciated.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

// Boost result_of library

// Copyright Douglas Gregor 2003-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
#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; };
};

template<typename T>
struct int_result_type_template { typedef int result_type; };

template<typename T>
struct int_result_of_template
{
  template<typename F> struct result;
  template<typename This, typename That> struct result<This(That)> { typedef int type; };
};

template<typename T>
struct int_result_type_and_float_result_of_template
{
  typedef int result_type;
  template<typename F> struct result;
  template<typename This, typename That> struct result<This(That)> { 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<int_result_type_template<void>(float)>::type, int>::value));
  BOOST_STATIC_ASSERT((is_same<result_of<int_result_of_template<void>(double)>::type, int>::value));
  BOOST_STATIC_ASSERT((is_same<result_of<int_result_of_template<void>(void)>::type, void>::value));
  BOOST_STATIC_ASSERT((is_same<result_of<const int_result_of_template<void>(double)>::type, int>::value));
  BOOST_STATIC_ASSERT((is_same<result_of<volatile int_result_of_template<void>(void)>::type, void>::value));
  BOOST_STATIC_ASSERT((is_same<result_of<int_result_type_and_float_result_of_template<void>(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;
}


? result_of_patch.txt
Index: result_of.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/utility/result_of.hpp,v
retrieving revision 1.7
diff -b -d -u -r1.7 result_of.hpp
--- result_of.hpp 16 May 2006 22:55:27 -0000 1.7
+++ result_of.hpp 7 Jan 2007 05:16:46 -0000
@@ -29,28 +29,25 @@
 
 BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
 
-template<typename F, typename FArgs, bool HasResultType> struct get_result_of;
+template<typename F, typename FArgs, bool HasResultType> struct result_of_impl;
 
 template<typename F, typename FArgs>
-struct get_result_of<F, FArgs, true>
+struct result_of_impl<F, FArgs, true>
 {
   typedef typename F::result_type type;
 };
 
 template<typename F, typename FArgs>
-struct get_result_of<F, FArgs, false>
+struct result_of_impl<F, FArgs, false>
   : F::template result<FArgs>
 {};
 
 template<typename F>
-struct get_result_of<F, F(void), false>
+struct result_of_impl<F, F(void), false>
 {
   typedef void type;
 };
 
-template<typename F, typename FArgs>
-struct result_of_impl : 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>))
Index: detail/result_of_iterate.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/utility/detail/result_of_iterate.hpp,v
retrieving revision 1.3
diff -b -d -u -r1.3 result_of_iterate.hpp
--- detail/result_of_iterate.hpp 16 May 2006 22:55:26 -0000 1.3
+++ detail/result_of_iterate.hpp 7 Jan 2007 05:16:46 -0000
@@ -21,21 +21,21 @@
 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_impl<F, F(BOOST_RESULT_OF_ARGS)> {};
+ : detail::result_of_impl<F, F(BOOST_RESULT_OF_ARGS), (detail::has_result_type<F>::value)> {};
 #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_impl<R (*)(BOOST_RESULT_OF_ARGS), FArgs>
+struct result_of_impl<R (*)(BOOST_RESULT_OF_ARGS), FArgs, false>
 {
   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_impl<R (&)(BOOST_RESULT_OF_ARGS), FArgs>
+struct result_of_impl<R (&)(BOOST_RESULT_OF_ARGS), FArgs, false>
 {
   typedef R type;
 };
@@ -47,7 +47,7 @@
          BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),typename T)>
 struct result_of_impl<R (T0::*)
                      (BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T)),
- FArgs>
+ FArgs, false>
 {
   typedef R type;
 };
@@ -57,7 +57,7 @@
 struct result_of_impl<R (T0::*)
                      (BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
                      const,
- FArgs>
+ FArgs, false>
 {
   typedef R type;
 };
@@ -67,7 +67,7 @@
 struct result_of_impl<R (T0::*)
                      (BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
                      volatile,
- FArgs>
+ FArgs, false>
 {
   typedef R type;
 };
@@ -77,7 +77,7 @@
 struct result_of_impl<R (T0::*)
                      (BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_ITERATION(),T))
                      const volatile,
- FArgs>
+ FArgs, false>
 {
   typedef R type;
 };


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk