我正在创建一个自定义的ostream类,下面的代码片段简要介绍了该类。我希望能够使用std::endl
,但编译器不允许我这样做。我不明白为什么。
#include <iostream>
struct Bar
{
};
template <typename T>
struct Foo
{
};
template <typename T, typename U>
Foo<T>& operator<<(Foo<T>& _foo, U&&)
{
return _foo;
}
int main()
{
Foo<Bar> f;
f << "aa" << std::endl;
}
gcc 4.7.1给我的错误是:
为什么不能推导出参数U?这不是
typeof(std::endl)
吗? 最佳答案
由于std::endl
是
namespace std {
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
}
您的类(class)不是从
basic_ostream
派生的,因此它无法工作。而且
basic_ostream
有basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&))
适用于
std::endl
等操纵器。