Boost logo

Boost :

Subject: Re: [boost] How to detect if f() returns void or not?
From: Eric Niebler (eric_at_[hidden])
Date: 2009-12-13 06:02:38


Eric Niebler wrote:
> Frédéric Bron wrote:
>> Eric Niebler wrote:
>>> I solved this problem once while writing a different trait and
>>> documented it
>>> here:
>>> http://www.boost.org/doc/libs/1_41_0/doc/html/proto/appendices.html#boost_proto.appendices.implementation.function_arity
>>>
>>
>> This does not check for return type but just ignores it with the
>> operator, trick. In the end, you do not know if fun(a,b) return void
>> or not.
>
> I find your lack of faith disturbing.
<snip>

Corrected version...

   #include <boost/static_assert.hpp>

   typedef char yes_type;
   typedef char (&no_type)[2];

   struct void_return
   {
     template<typename T>
     friend int operator,(T const &, void_return);
   };

   no_type check_is_void_return(int);
   yes_type check_is_void_return(void_return);

   // check to see if the expression fun(x,y) is void
   template<typename Fun, typename X, typename Y>
   struct returns_void
   {
     static Fun &fun;
     static X &x;
     static Y &y;
     static bool const value =
       sizeof(yes_type) ==
sizeof(check_is_void_return((fun(x,y),void_return())));
   };

   int main()
   {
     BOOST_STATIC_ASSERT((returns_void<void(int,int),int,int>::value == 1));
     BOOST_STATIC_ASSERT((returns_void<int(int,int),int,int>::value == 0));
   }

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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