Posts
Simplify Ternary Operators
One of my pet peeves in C/C++/C#/Java programming is the use of the ternary operator
?:
with boolean values. It’s almost always redundant, makes code harder to read and can be easily replaced by a simpler boolean expression that also has the advantage of being “branch-free” in some languages, that is, do not require the CPU to select a path of execution based on a condition, which can be fairly expensive. The best compilers will optimize them away in a manner similar to what I describe here, but they still don’t improve code readability.Typesafe Enum Class Bitmasks in C++
I’ve been working on virt86 for a few weeks now, and some of the things I decided to focus on were type safety and making good use of modern C++ features. I did many things to reduce the chances for users to shoot their own foot with the library, such as deleting move and copy constructors and assignment operators from classes to make sure there is only one instance obtained from a known source, deleting or hiding the address operator
&
so that users cannot get a pointer to objects that are not meant to bedelete
d and usingenum class
for type-safe enumerations.I figured it would be interesting to use
enum class
es as bitmasks for their type-safety. Unfortunately, you cannot use them as is with bitwise operators, but you can use other C++ features to make them behave like a bitmask type.Hello, World!
Hello there visitor, and welcome to my site!
What is this about? It’s about computer science and software development topics or projects I feel like discussing.
subscribe via RSS