Boost logo

Boost Users :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2008-01-10 20:38:48


Steven Watanabe wrote:
> AMDG
>
> Tobias Schwinger <tschwinger <at> isonews2.com> writes:
>
>>> I don't think the comma operator
>>> is a completely fool-proof way to detect void.
>> It is.
>>
>>> It could cause an
>>> ambiguity if the other type also defines an overloaded comma operator.
>> No, because the operator is binary and one operand can have a reserved
>> type which is only internally used by the test code.

I hope I haven't promised too much :-)...

>
> Ok. Let's make this concrete. Here is what I imagine:

<code>

Try the attached code - does it work for you?

Regards,
Tobias


#include <iostream>
#include <cassert>

template< typename T >
struct reserved
{
    char i[2];
    template< typename U >
    friend char operator,(T const&, reserved<U> const&);
};

#define IS_VOID(expr,target_type) \
    (sizeof(( (expr) , reserved<target_type>() )) != 1)

//

void void_();
int nonvoid();

struct X {};
struct Y { Y(X); Y(); };

template<class T>
T operator,(X, T);

//

int main()
{
    assert( IS_VOID(void_(),int) );
    assert( IS_VOID(void_(),X) );
    assert( IS_VOID(void_(),Y) );
    assert( !IS_VOID(nonvoid(),int) );
    assert( !IS_VOID(nonvoid(),X) );
    assert( !IS_VOID(nonvoid(),Y) );
    assert( !IS_VOID(X(),int) );
    assert( !IS_VOID(X(),X) );
    assert( !IS_VOID(X(),Y) );
    assert( !IS_VOID(Y(),int) );
    assert( !IS_VOID(Y(),X) );
    assert( !IS_VOID(Y(),Y) );
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net