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 05:57:00

Frédéric Bron wrote:
>> I solved this problem once while writing a different trait and documented it
>> here:
> 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.

   #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) ==

   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

Boost list run by bdawes at, gregod at, cpdaniel at, john at