Jos*_*vin 5 c++ gcc sfinae c++11 c++14
我正在使用C++ 03方法在编译时检测函数的存在.我必须使用这个方法而不是void_t方法,即使我使用C++ 14因为我必须支持GCC 4.9,并且在使用void_t方法时出错(奇怪的是只有Ubuntu 14的GCC 4.9有这个问题,而不是Fedora的,但它在GCC5 + AFAICT中全面修复.
具体来说,我正在检查是否存在,operator<<(std::ostream&, const T&)以便我可以使用任何类型的漂亮打印功能.当函数被调用时,如果类型支持它,您将获得常规的ostream输出,并且当未定义运算符时,您将收到有关没有实现的回退消息.代码在底部.
到目前为止,这对我来说非常有效,直到我遇到第三方库定义的类型,我无法改变.该类型具有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;
}
Run Code Online (Sandbox Code Playgroud)
好吧,你不必使用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 {};
Run Code Online (Sandbox Code Playgroud)
在Wandbox 的 GCC 4.9上运行良好。