Overloading operator<< to take wathever type

Hi everyone, this is the code I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class MyCout {
public:
	MyCout(string filename) :mFilename{filename} {}

	MyCout& operator<<(const string s)
	{
		mMyFile.open(mFilename, ofstream::out);
		
		cout << s; 
		mMyFile << s;
		
		mMyFile.close();
		return *this;
	}
private:
	ofstream mMyFile;
	string mFilename;
};

void debugConsole(bool create = true);

int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int show)
{
#if defined(DEBUG) || defined(_DEBUG)
	debugConsole();
#endif

	MyCout stream1("outputFile1.txt");
	stream1 << "test_test_test\n";

	system("pause");
	return 0;
}

void debugConsole(bool create)
{
	if (create)
	{
		//Create Console
		FILE* stream;
		AllocConsole();
		freopen_s(&stream, "conout$", "w", stdout);
	}
}


What I want to achieve is to basically cout bot on the debugConsole and on a file with a single call to my class. It works in the example above because operator<< is overloaded for strings, but if I want to pass a number now it requires a second overload, if I want to pass <<endl; it requires one more and so on.
Therefore I was wondering, would be possible to do some template trickery or some other thing in order to have my operator<< accept wathever type? (since behind the since cout is doing the work inside of it and has all the proper overloads already set up)

Thanks :)
Last edited on
boost::iostreams::tee_device http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/index.html

Usage example: https://stackoverflow.com/a/999218


A home-grown tee_stream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

template < typename C, typename T = std::char_traits<C> >

struct basic_teebuf : public std::basic_streambuf<C,T>
{
    typedef std::basic_streambuf<C,T> streambuf_type ;
    typedef typename T::int_type int_type ;

    basic_teebuf( streambuf_type* buff_a, streambuf_type* buff_b )
            : first(buff_a), second(buff_b) {}

    protected:
        virtual int_type overflow( int_type c )
        {
            const int_type eof = T::eof() ;
            if( T::eq_int_type( c, eof ) ) return T::not_eof(c) ;
            else
            {
                const C ch = T::to_char_type(c) ;
                if( T::eq_int_type( first->sputc(ch), eof ) ||
                    T::eq_int_type( second->sputc(ch), eof ) )
                        return eof ;
                else return c ;
            }
        }

        virtual int sync()
        { return !first->pubsync() && !second->pubsync() ? 0 : -1 ; }

    private:
        streambuf_type* first ;
        streambuf_type* second ;
};

template < typename C, typename T = std::char_traits<C> >
struct basic_teestream : public std::basic_ostream<C,T>
{
    typedef std::basic_ostream<C,T> stream_type ;
    typedef basic_teebuf<C,T> streambuff_type ;

    basic_teestream( stream_type& first, stream_type& second )
         : stream_type(&stmbuf), stmbuf( first.rdbuf(), second.rdbuf() ) {}

    basic_teestream( streambuff_type* first, streambuff_type* second )
         : stream_type(&stmbuf), stmbuf(first,second ) {}

    ~basic_teestream() { stmbuf.pubsync() ; }

    private: streambuff_type stmbuf ;
};

typedef basic_teebuf<char> teebuf ;
typedef basic_teestream<char> teestream ;

#include <fstream>
#include <iomanip>

int main()
{
    std::ofstream file( "out.txt" ) ;
    teestream tee( std::cout, file ) ; // tee to both file and stdout

    tee << "hello world!\n" << 1234 << '\n'
        << std::fixed << std::setprecision(2) << 123.4567 << '\n' << std::flush ;
}

Thanks for the reply JLBorges, though actually while waiting for a reply, I think I've ended up with a solution xD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct MyCout {
	MyCout(string filename) { myFile.open(filename, ofstream::out); }
	inline stringstream& print() {
				       string s; stream >> s;
				       cout << s; myFile << s;
				       return stream;
	}
	stringstream stream;
	ofstream myFile;
};
ostream& operator<<(ostream& stream, MyCout& debug) { return debug.print(); }

int main()
{
        MyCout debug("outputFile1.txt");
	debug.stream << "test_test_test" << endl << debug;
}


I just need to concatenate that << debug; at the end to have it flush the stringstream into both file and cout. Since it actually does the job, I think for now I'll stick with it :P
Last edited on
Topic archived. No new replies allowed.