How to redirect std C++ streams to Visual Studio debug output

Discovered this trick for the first time. You can use AllocConsole() to redirect ANY std stream to debug console.

In most cases people use either TRACE() statement (but you have include MFC or ATL for that) or Win API OutputDebugString().

With AllocConsole() your code may look like this:

   freopen("CONOUT$", "w", stdout);

That’s it!

Learning something new every day. 😀

C++. Concepts

One more reading on what’s going on with the C++ standard. Concepts were proposed before, but now TS (Technical Specification) became concrete and may become a part of next standard. One thing I specifically like about concepts is that you can specify template constraints. Why do you need them? It’s simple. Constraints will help you to get human readable error messages from compiler instead of 100 lines garbage that current STL errors generated by compilers.

Anyway, Andrew Sutton’s article on what’s going on with concepts and why we need them:

Writing Good C++

Yes, C++ is evolving. 🙂 C++14 is almost here and C++17 is rising in the horizon. I find it amazing that so far all attempts to make language somewhat more friendly and safer looked like islands of resistance created by small teams all around the world. And they were disconnected. Coding standards, numerous books on good practices, – we saw them all. We have not seen the C++ father Bjarne Stroustrup saying anything on this subject. I obviously ignoring his book “A Tour of C++” and his conference speeches, but those were conceptually still islands in the ocean. C++ is huge, standard is 300+ pages, last Bjarne’s book is more like a monument to the greatness of C++, but can’t be used neither as a manual nor tutorial.

Here we go. New initiative from The Father to define good practices and guidelines for modern C++.

Here’s his deck from keynote:

Interestingly, right from the beginning they came up with “good library” called GSL. Static syntax validation tools is coming (good!).

Github link to GSL:

His speech on this subject at CppCon 2015. I like Bjarne’s philosophical view on coding standards. Yes, we hate them! 🙂

Swift critique from Rust designer

In nutshell Rust is another evolution of C/C++ by Mozilla Foundation. Another attempt to create a cleaner and safer language with minimal performance overhead (POD and static types, move semantics, well defined object format, small runtime, efficient use of pointers and references etc). Along the line with D, Go, Swift and likes. I’m not at the position of covering all ins and outs of Rust, as I’ve not written a single line of code in it, but since I’ve started gradual migration toward Swift, I had to keep eye on its conceptual competitors (or relatives). While doing that, I found good article from Rust designer Graydon Hoare about where he compared Swift with Rust. Pretty interesting overview. You can find it here.

More on Rust:

C++ STL containers with custom hash function

That’s a bit unusual trip to side roads for me. I’ve been working with C++ for many years and considered myself pretty experienced with STL containers. I remembered (I thought) how to extend a class so. C++ is a language that does not forgive hiatuses. My approach to custom hash function was similar to what highest ranking answer in this stackoverflow answer is showing:

First of all, it’s a bit outdated, because with introduction of C++11 tr1 namespace became obsolete. Secondly, to my shame, I was unable to make my example compile. So after some head scratching, I decided to go for even simpler solution. It may be not as beautiful as what you can find in C++ books, but it works. So here it is.

Our sample class looks like this:

struct Square
unsigned long long x;
unsigned long long y;

Square(unsigned long long _x, unsigned long long _y) : x(_x), y(_y) {};

To make class work with STL containers like map and set you need to implement a method returning a hash value of the object.

struct SquareHash
size_t operator() (const Square& square) const
return square.y * 1000 + square.y;

Besides that you either need to implement a function that compares two values. In case of regular std::map or std::set it should be “<” and in case of unordered_map and unordered_set – “==”. You can do it by implementing an operator< or operator== for operands of your custom class, but I found it even clearer syntactically to implement a class:

struct SquareEqual
bool operator() (const Square&amp; lhs, const Square&amp; rhs) const
return (lhs.x == rhs.x) &amp;&amp; (lhs.y == rhs.y);

Just in case, look at unordered_map declaration:

template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash, // unordered_map::hasher
class Pred = equal_to, // unordered_map::key_equal
class Alloc = allocator < pair<const Key,T> >// unordered_map::allocator_type
class unordered_map;

That’s it! Now you can declare an unordered_map:

unordered_set <Square, SquareHash, SquareEqual> visitedSquares;

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.