Я не уверен, что этот код не скомпилируется.
Пример кода, с которым я работаю:
#include <iostream>
using std::cout;
using std::endl;
class Foo {
public:
template<typename T>
Foo& operator<<(const T& t) {
cout << t;
return *this;
}
};
int main() {
Foo foo;
foo << "Hello World"; // perfectly fine
foo << endl; // shit hits the fan
return 0;
}
Это ошибка:
test.cpp:19:12: error: no match for ‘operator<<’ in ‘foo << std::endl’
test.cpp:19:12: note: candidates are:
test.cpp:10:14: note: template<class T> Foo& Foo::operator<<(const T&)
test.cpp:10:14: note: template argument deduction/substitution failed:
test.cpp:19:12: note: couldn't deduce template parameter ‘T’
Я смущен тем, почему он не может заменить тип функции endl
(ostream& (*)(ostream&)
) на T
, где явно можно делать это, когда вы указываете cout << endl;
Меня также озадачивает, что это решает проблему [отредактировано]
Foo& operator<<(ostream& (*f)(ostream&)) {
cout << f;
return *this;
}
В случае, если вопрос не ясен, я спрашиваю, почему он не мог вывести шаблон в первую очередь.
std::basic_ostream<C,CT>
или не реализуете streambuf? - person sehe   schedule 23.02.2013