I have a class which inherits from ofstream
. I want to overload the insertion operator so that it can be a drop in repalcement for ofstream.
The first overload is
template<class T>
MyClass& Myclass::operator<<(const T& in);
and to try and handle the manipulators like std::endl
template<
template<class, class> class Outer,
class Inner1,
class Inner2
>
MyClass& Myclass::operator<<(Outer<Inner1, Inner2>& (*foo)(Outer<Inner1, Inner2>&));
If I try to compile with:
Myclass output; output << 3 << "Hi";
Then everything works fine, but when I try to add std::endl
Myclass output; output << 3 << "Hi" << std::endl;
temp.cpp: In function 'int main()':
temp.cpp:10: error: no match for 'operator<<' in '((Stream*)ss. Stream::operator<< [with T = int](((const int&)((const int*)(&3)))))->Stream::operator<< [with T = char [3]](((const char (&)[3])"Hi")) << std::endl'
/usr/include/c++/4.1.2/bits/ostream.tcc:657: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.1.2/bits/ostream.tcc:597: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
I especially don't understand why in the error printout there is a const int*
.
This is also an attempt to learn some more about templates, but I am also trying to cover more manipulators with a single piece of code.
EDIT SSCCE:
#include <fstream>
#include <iostream>
class Myclass : public std::ofstream {
//class Stream {
private:
public:
template<class T>
Myclass& operator<<(const T& data_in) {
std::cout << data_in;
return *this;
}
template<
template<class, class> class Outer_T,
class Inner_T1,
class Inner_T2
>
Myclass& operator<<(Outer_T<Inner_T1, Inner_T2>& (*foo)(Outer_T<Inner_T1, Inner_T2>&)) {
return foo(*this);
}
};
int main() {
Myclass output;
output << 3 << "Hi";
output << 3 << "Hi" << std::endl;
}