- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!