如何使用enable_if有条件地定义<<运算符
How to use enable_if to conditionally define a << operator
我想定义一个<<类集合的操作符;集合是开放的,但是所有的成员都有一个共同的标记基类,并且都有一个成员函数std::string String() const
。基本上,我得到的是:
class Tag {};
class Obj : public Tag
{
public:
std::string String() const { return "specialized"; }
};
template <typename T>
typename std::enable_if<std::is_base_of<Tag, T>::type, std::ostream>::value& operator<<( std::ostream& dest, T const& source)
{
dest << source.String();
return dest;
}
int
main()
{
std::cout << typeid(std::enable_if<std::is_base_of<Tag, Obj>::value, std::ostream>::type).name() << std::endl;
std::string s( "generic" );
Obj e;
std::cout << e << std::endl;
std::cout << s << std::endl;
return 0;
}
这不起作用:对于g++(版本4.8.3,用-std=c++11
调用),我得到错误消息:
enableIf.cc: In function 'int main()':
enableIf.cc:55:18: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
std::cout << e << std::endl;
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/iostream:39:0,
from enableIf.cc:8:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/ostream:602:5: error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Obj]'
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
我不能弄清楚,因为没有任何右值引用;编译器似乎碰到了标准库中std::ostream&&
的泛型过载。
在MSC (VS 2013)中,错误消息要详细得多,但它以:
开头。enableIf.cc(55) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Obj' (or there is no acceptable conversion)
,然后继续列出了许多可能的函数,它们都在标准库中。
(在我的实际代码中,第55行对应于std::cout << e << std::endl;
行。)
在这两种情况下,编译器似乎都拒绝了我的重载函数。但是,如果我注释掉<<
行,代码就会编译,并且main
中第一行的输出值似乎是正确的(至少使用MSC— g++的输出是So
,不管这意味着什么)。
假设两个编译器一致,我假设我的代码中有错误,但我不知道是什么。你是怎么做到的?(FWIW:我同样很高兴,甚至更高兴,与一个解决方案,产生重载的所有类型有一个成员函数std::string Type::String() const
)
我很确定你是这个意思:
template <typename T> // here here
typename std::enable_if<std::is_base_of<Tag, T>::type, std::ostream>::value&
operator<<( std::ostream& dest, T const& source)
等于:
template <typename T>
typename std::enable_if<std::is_base_of<Tag, T>::value, std::ostream>::type&
operator<<( std::ostream& dest, T const& source)
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 1<<31产生误差;'<<'表达式未定义”;
- 当运算符<存在时,为什么要定义 LT?
- 重载& lt; & lt;用于打印自定义异常的操作符
- & lt; & lt;使用继承和模板定义