This is a small wrapper for a system of binnacles; the used library is in C, so I have simply added an interface to std::stream
:
#include <sstream>
#include <iostream>
class ILogger;
class Stream {
friend class ::ILogger;
std::stringstream m_ss;
Stream( const Stream & );
Stream &operator=( const Stream & );
public:
~Stream( ) {
std::cout << m_ss.str( ) << std::endl;
}
inline Stream( ) { }
template< typename T > inline Stream &operator<<( const T &val ) {
m_ss << val;
return *this;
}
};
class ILogger {
public:
template< typename T > inline Stream operator<<( const T &val ) {
Stream s;
s << val;
return s;
}
};
int main( ) {
ILogger log;
log << "Hola" << " mundo " << "cruel";
return 0;
}
The library uses functions at printf
, so we just generate a string using a std::stringstream
using an auxiliary class and, when destroying the instance of that auxiliary, is when you call the true function of C.
To my surprise, the code works!
I say this because I expected a compilation error, having Stream::Stream( const Stream & )
declared as private
. I hoped that, when leaving the field, the copy constructor and the destructor would be invoked. I have not said it yet, but this is for C ++ 98.
template< typename T > inline Stream operator<<( const T &val ) {
Stream s;
s << val;
return s; // salimos del ámbito. 's' se pierde
}
I am compiling with g++ 7.2
, with the order g++ -std=c++98 -Wall -Wextra -pedantic
.
- The question is: Is this the expected behavior, or is it a feature of my compiler? Can I trust is to get the same results in older compilers?
Note : The goal of that code is to run on very old machines, with SDK no longer supported ... and I do not want last-minute surprises: -O