How can I detect if a type can be streamed to an std::ostream?

It’s apparently this overload of operator<< that’s stepping in your way and making the expression in traling return type valid: template< class CharT, class Traits, class T > basic_ostream< CharT, Traits >& operator<<( basic_ostream<CharT,Traits>&& os, const T& value ); See (3) on this reference page. It’s a simple forwarder (calling os << value) that was …

Read more

How can I properly overload the

I am just telling you about one other possibility: I like using friend definitions for that: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) { […] } }; } The function will be automatically targeted into the surrounding namespace Math (even though its definition appears within the …

Read more

Prevent scientific notation in ostream when using

To set formatting of floating variables you can use a combination of setprecision(n), showpoint and fixed. In order to use parameterized stream manipulators like setprecision(n) you will have to include the iomanip library: #include <iomanip> setprecision(n): will constrain the floating-output to n places, and once you set it, it is set until you explicitly unset …

Read more

DataContractSerializer – how can I output the xml to a string (as opposed to a file)

Something like this – put your output into a MemoryStream and then read that back in: public static string DataContractSerializeObject<T>(T objectToSerialize) { using(MemoryStream memStm = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(memStm, objectToSerialize); memStm.Seek(0, SeekOrigin.Begin); using(var streamReader = new StreamReader(memStm)) { string result = streamReader.ReadToEnd(); return result; } } }

std::cout won’t print

Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will be flushed unless you manually flush it yourself. std::cout << “Hello” << std::endl; std::endl will output a newline and flush the stream. Alternatively, std::flush will just do the flush. Flushing …

Read more