Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect operator at compile time without implicit conversions

I am using the C++03 method to detect the presence of a function at compile time. I have to use this method rather than the void_t method even though I'm using C++14 because I have to support GCC 4.9, and it errors when using the void_t method (strangely only Ubuntu 14's GCC 4.9 has this problem, not Fedora's, but it's fixed across the board in GCC5+ AFAICT).

Specifically I am checking for the presence of operator<<(std::ostream&, const T&) so that I can have a pretty print function that takes any type. When the function gets called, you get the regular ostream output if the type supports it, and you get a fallback message about there being no implementation when the operator is not defined. Code at the bottom.

This has worked pretty well for me so far, until I ran into a type defined by a 3rd party library that I can't change. The type has implicit conversion operators to both bool and float. This means when the SFINAE check is done to see if s << t is valid I get a compiler error because s << t is ambiguous. In this case I'd prefer for it to just report there is no implementation like normal, rather than try to pick an implicit conversion. Is there a way to change the SFINAE check to make this possible? I have checked and the void_t method with GCC5 appears to do what I want (commented out in the code below), but I can't use it yet for the aforementioned reasons.

Test case:

#include <iostream>
#include <typeinfo>
#include <type_traits>

namespace detail {

    namespace has_ostream_operator_impl {
        typedef char no;
        typedef char yes[2];

        struct any_t {
            template<typename T> any_t( T const& );
        };

        no operator<<( std::ostream const&, any_t const& );

        yes& test( std::ostream& );
        no test( no );

        template<typename T>
        struct has_ostream_operator {
            static std::ostream &s;
            static T const &t;
            // compiler complains that test(s << t) is ambiguous
            // for Foo
            static bool const value = sizeof( test(s << T(t)) ) == sizeof( yes );
        };
    }

    template<typename T>
    struct has_ostream_operator :
        has_ostream_operator_impl::has_ostream_operator<T> {
    };

    // template<class, class = std::void_t<>>
    //     struct has_ostream_operator : std::false_type {};

    // template<class T>
    // struct has_ostream_operator<
    //     T,
    //     std::void_t<
    //         decltype(std::declval<std::ostream&>() << std::declval<const T&>())>>
    //     : std::true_type {};

}

template<class X>
std::enable_if_t<
    detail::has_ostream_operator<X>::value
    && !std::is_pointer<X>::value>
prettyPrint(std::ostream& o, const X& x)
{
    o << x;
}

template<class X>
std::enable_if_t<
    !detail::has_ostream_operator<X>::value
    && !std::is_pointer<X>::value>
prettyPrint(std::ostream& o, const X& x)
{
    o << typeid(x).name()
      << " (no ostream operator<< implementation)";
}

template<class X>
void prettyPrint(std::ostream& o, const X* x)
{
    o << "*{";
    if(x) {
        prettyPrint(o, *x);
    } else {
        o << "NULL";
    }
    o << "}";
}

struct Foo {
    operator float() const {
        return 0;
    }

    operator bool() const {
        return false;
    }
};

struct Bar {};

int main()
{
    Bar x;
    Foo y;
    prettyPrint(std::cout, 6); // works fine
    std::cout << std::endl;

    prettyPrint(std::cout, Bar()); // works fine
    std::cout << std::endl;

    prettyPrint(std::cout, x); // works fine
    std::cout << std::endl;

    prettyPrint(std::cout, &x); // works fine
    std::cout << std::endl;

//    prettyPrint(std::cout, y); // compiler error
    std::cout << std::endl;

    return 0;
}
like image 330
Joseph Garvin Avatar asked Sep 25 '22 22:09

Joseph Garvin


1 Answers

Well, you don't have to use void_t (which is syntax sugar, anyway). Bog-standard expression SFINAE is supported by GCC 4.9:

template <typename, typename = void>
struct has_ostream_operator : std::false_type {};

template <typename T>
struct has_ostream_operator<T, decltype(void(std::declval<std::ostream&>() << std::declval<const T&>()))>
    : std::true_type {};

works fine on Wandbox's GCC 4.9.

like image 148
Columbo Avatar answered Sep 29 '22 05:09

Columbo