Boost logo

Boost :

Subject: Re: [boost] How to detect if f() returns void or not?
From: Barend Gehrels (barend_at_[hidden])
Date: 2009-12-13 05:42:13


Hi,

> I would like to detect if the return type of a function is void or not
> within C++03 standard?
>

You can specialize on function type (provided that the arguments are known).

template <typename T> struct check_void : boost::false_type {};
template <> struct check_void<void (*)()> : boost::true_type {};

void returns_void() {}
int returns_int() { return 0; }

template <typename F>
void test_type(F function)
{
    std::cout << typeid(F).name()
        << (check_void<F>::value ? " true" : " false")
        << std::endl;
}

int test_main(int, char* [])
{
    test_type(returns_void);
    test_type(returns_int);
    ...

> I need that to implement new type traits to detect if types can be
> compared and to detect for example operator< returning void.

I don't know if you can use it like this on an operator< returning void.

Regards, Barend


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