Tom*_*Tom 0 c++ templates list operator-keyword
我试图在一个课程作品上展示多样性,并希望使用<<运算符轻松地将变量添加到列表中.例如:
UpdateList<string> test;
test << "one" << "two" << "three";
Run Code Online (Sandbox Code Playgroud)
我的问题是,每个<<运算符的例子都与ostream有关.
我目前的尝试是:
template <class T> class UpdateList
{
...ect...
UpdateList<T>& operator <<(T &value)
{
return out;
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我是如何实现这一点的,或者在C++中实际上是不可能的?
你应该用const T& value.下面的代码片段应该可以正常工作
UpdateList<T>& operator << (const T& value)
{
// push to list
return *this;
}
Run Code Online (Sandbox Code Playgroud)
要么
UpdateList<T>& operator << (T value)
{
// push to list
return *this;
}
Run Code Online (Sandbox Code Playgroud)
在C++ 11中(感谢rightfold)