c++ - 在没有隐式转换的情况下在编译时检测运算符

标签 c++ c++11 gcc c++14 sfinae

我正在使用 C++03 method to detect the presence of a function at compile time .我必须使用此方法而不是 void_t 方法,即使我使用的是 C++14,因为我必须支持 GCC 4.9,并且在使用 void_t 方法时出错(奇怪的是只有 Ubuntu 14 的 GCC 4.9 有这个问题,而不是 Fedora 的,但它已在 GCC5+ AFAICT 中全面修复)。

具体来说,我正在检查 operator<<(std::ostream&, const T&) 是否存在这样我就可以拥有一个 pretty-print 功能,可以接受任何类型。当函数被调用时,如果类型支持它,您会得到常规的 ostream 输出,并且当运算符未定义时,您会收到一条关于没有实现的回退消息。代码在底部。

到目前为止,这对我来说效果很好,直到我遇到无法更改的第 3 方库定义的类型。该类型具有到 bool 和 float 的隐式转换运算符。这意味着当完成 SFINAE 检查以查看是否 s << t有效 我收到编译器错误,因为 s << t是模棱两可的。在这种情况下,我更希望它只报告没有正常的实现,而不是尝试选择隐式转换。有没有办法更改 SFINAE 检查以使其成为可能?我已经检查过,GCC5 的 void_t 方法似乎可以执行我想要的操作(在下面的代码中注释掉),但由于上述原因我还不能使用它。

测试用例:

#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;
}

最佳答案

好吧,您不必使用 void_t(无论如何,这是语法糖)。 GCC 4.9 支持 bog 标准表达式 SFINAE:

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 {};

Wandbox's GCC 4.9 上运行良好.

关于c++ - 在没有隐式转换的情况下在编译时检测运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317701/

相关文章:

c++ - 如何在 C++ 中为 Node.JS 实现动态变量

C - 强制字符串参数在只读内存中

c - 在 linux 主机上遇到 "floating point exception"错误

c++ - 将随机数放入缓冲区以写入文件的有效方法是什么?

c++ - 确定 8 字节数据的性质

c++ - 修复重载运算符 '+' 的使用不明确?

c++ - 我可以使用 Qt 线程 ID 为每个线程创建唯一的缓存吗?

c++ - 自动扣除失败并显示消息 "inconsistent deduction for auto return type"

c++ - QCache 和 std::shared_ptr

在 glibc 中找不到 iso646.h