【发布时间】:2017-10-16 14:34:54
【问题描述】:
我有
一个类Value,可以用不同的类型(Foo, Bar, int,...)构造。
类 Value 应该有像
我在类定义之外添加了运算符。
我必须要以下代码:
#include <iostream>
struct Foo {
};
struct Bar {
};
struct Value {
template<typename T>
Value(T) {
}
};
std::ostream &operator<<(std::ostream &os, const Bar &) {
return os << "Bar\n";
}
std::ostream &operator<<(std::ostream &os, const Value &value) {
auto visitor = [&](auto a) -> decltype(os << a) {
return os << a;
};
// Works
visitor(Bar{});
// Infinity call of this function with Value.
visitor(Foo{});
return os;
}
int main() {
std::cout << Value(1);
return 0;
}
问题:如果底层类型没有实现操作符Value的操作符no match for call operator 以将 SFINAE 与我的访问者模式一起使用(此处未显示)。
我需要的是这样的:
[&](auto a) -> std::enable_if_t<addressof?(os << a) != addressof?(os << Value{})>::value> {
return os << a;
};
如果功能相同,则禁用此 lambda。这可能吗?
没有价值的解决方案:
- 明确价值
- 禁止使用 Foo 构造值
【问题讨论】:
标签: c++ templates c++17 sfinae enable-if