Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用运算符转发整个类_C++_Templates - Fatal编程技术网

C++ 使用运算符转发整个类

C++ 使用运算符转发整个类,c++,templates,C++,Templates,我想知道。我遇到了一个问题,这里是一个小报告。基本上,我想把一切都转发出去。问题是,使用第一个 如果这样做,则也可以对const对象调用此函数: void f(const FowardIt &o)//note: inside the function, o is an const object! { o << 1; o << SomeUserStruct(); } void f(const FowardIt&o)//注意:在函数中,o是一个con

我想知道。我遇到了一个问题,这里是一个小报告。基本上,我想把一切都转发出去。问题是,使用第一个 如果这样做,则也可以对
const
对象调用此函数:

void f(const FowardIt &o)//note: inside the function, o is an const object!
{
    o << 1;
    o << SomeUserStruct();
}
void f(const FowardIt&o)//注意:在函数中,o是一个const对象!
{

o回答得很好。但是如果我使方法常量,我也必须使返回常量。这将是不合逻辑的(对于常规流)。@acidzombie24:是的。我也添加了这个!你知道这有什么问题吗?十六进制工作,所以我们知道函数正在向前。但是endl不是。
template<typename T> FowardIt& operator<<(const T&t)
                                        //^^^^^ put const here
template<typename T> 
const FowardIt& operator<<(const T&t) const
^^^^^                      ^^^^^      ^^^^^
  |                          |          |
  |                          |          put const here as well
  |                          put const here
  |
  You've to make the return-type also const
  since it can't return non-const reference anymore
void f(const FowardIt &o)//note: inside the function, o is an const object!
{
    o << 1;
    o << SomeUserStruct();
}