c++ How to implement self referencing type

Drawing Hands is a lithograph by the Dutch artist M. C. Escher
Drawing Hands is a lithograph by the Dutch artist M. C. Escher

What is it? A type (struct or class) whose instance contains one or more references to the instance of the same type.

A sketch:

// Self Referencing type
struct SR { 

   char state;
const SR & ref;

};

Why would anybody do this? Continue reading “c++ How to implement self referencing type”

C++ How to simply handle variable number of function arguments

This is the first of posts, I am going to write primarily for C++ beginners.  Where this title “beginner” is a misnomer in the context of C++. To become a solid C++ beginner takes time and effort.

This is not a good situation. And the reason why, among other reasons, C++ creator (Bjarne Stroustrup) has written very short but, I think a very important “message to all” called  “Remember the Vasa!“. Please read it. Continue reading “C++ How to simply handle variable number of function arguments”

C++ Convert any character sequence to any standard string

Four standard char types can be transformed to each other
Four standard char types can be transformed to each other

(Note: this is the second part of C++ : codecvt deprecated. Panic? )

Update: This is not a foreign language translator or some such code.  This is a standard  C++ 17 utility to transform  the core character sets between each other.  The first 127 characters, that is. As a such it is remarkably useful and simple. For full blown, locale aware solutions please look elsewhere, starting from here.  End of update.

Standard C++ std lib, is one very complete and useful library. But there are times when you do realize you can build one or two of very simple utilities on top of it.

Simple but sometimes surprisingly powefull. Continue reading “C++ Convert any character sequence to any standard string”