Boost logo

Boost :

Subject: Re: [boost] [type_traits] common_type and SFINAE
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2012-10-05 14:08:03


Hello all,

One a separate note, why the following SFINAE impl of
EqualityComparable returns 0 on std::common_type<int, int>::type?

    EqualityComparable<int>::value // 1 -- ok
    EqualityComparable<std::common_type<int>::type>::value // 1 -- ok

    EqualityComparable<std::common_type<int, int>::type>::value // 0 -- why??

Code:

namespace std { class type_info; }

#include <boost/type_traits/detail/yes_no_type.hpp>
#include <iostream>
#include <type_traits>

template< typename _1 >
struct EqualityComparable
{
    struct requires0 {
        template< typename T >
        static boost::type_traits::yes_type check (
            T a, T b,
            char (*) [sizeof(decltype( bool{a == b} ))]
        );
        template< typename T >
        static boost::type_traits::no_type check ( ... );

        template< typename T >
        struct sizeof_check {
            T a; T b;
            static size_t const value = sizeof check<_1>(a , b , 0);
        };

        static bool const value = sizeof(boost::type_traits::yes_type) ==
                sizeof_check< _1 >::value;
    };
    static bool const value = requires0::value;
};

int main ( void )
{
    std::cout << EqualityComparable<int>::value << std::endl; // 1 -- ok
    std::cout << EqualityComparable<std::common_type<int>::type>::value
            << std::endl; // 1 -- ok
    std::cout << EqualityComparable<std::common_type<int, int>::type>::value
            << std::endl; // 0 -- why??
    return 0;
}

I'm using Clang++ 3.2.

Thanks,
--Lorenzo


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