Boost logo

Boost :

Subject: Re: [boost] How to detect if f() returns void or not?
From: Chard (boost_at_[hidden])
Date: 2009-12-14 09:25:40


> Eric Niebler wrote:
>
> #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));
> }

That's a nice trick. It would be good to see this, and Joel's stuff (from the
vault), put out as an introspection library.

FWIW, MSVC71 coughs unless the operator, is given an implementation, i.e.

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

[shrug]


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