이 코드가 컴파일되지 않을지 확실하지 않습니다.스트림 조작기에 대한 템플릿 유형 공제
내가 함께 일하고 있어요 예제 코드 :
#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;
}
이 오류입니다 : 나는 endl
의 기능 유형을 대체 할 수없는 이유에 혼란 스러워요
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’
(ostream& (*)(ostream&)
) T
에 대해 지정한 경우 분명히 괜찮 으면 cout << endl;
찾을 수 있습니다. addi 전통적으로이가 처음에 템플릿을 추론 할 수없는 이유를 질문이 명확하지, 내가 부탁 해요한다 [편집] 문제 경우
Foo& operator<<(ostream& (*f)(ostream&)) {
cout << f;
return *this;
}
해결되는 수수께끼.
달성하려는 목표는 무엇입니까? 'std :: basic_ostream'또는 streambuf 구현에서 파생되지 않은 특별한 이유가 있습니까? –
sehe
마지막 질문 : ** 여러 오버로드 **가 적용 가능하므로 템플릿을 추론 할 수 없었습니다. 따라서 함수 참조 사례가 모호했습니다. – sehe