Why I prefer inline forward-declares in C++

Time for a short post on how I usually do the humble forward declare in C++. I guess this is not something new but it is something I usually do not see in others code so it feels worth sharing.

So lets start of with just defining what we are talking about just to get everyone on the same page, we are talking about declaring only a class/struct name to the compiler and not having to provide the entire class/struct declaration. Mostly used as a compile-time optimization or to handle circular deps etc.

Read More

Compile-time hashes in c++, im not convinced!

I recently read a blogpost about compile-time string-hashes and constexpr and I’m still not convinced and see no real reason to leave my old and true friend the script :)

So first of lets look at the problem we want to solve. We want a way to do things like this and not pay the runtime cost ( and in this case just compile! ).

void my_func( uint32_t val )
{
    switch( val )
    {
        case HashOfString("some_string"):
            do_some_stuff();
            break;
        case HashOfString("some_other_string"):
            do_some_other_stuff();
            break;
    }
}

Simple enough. What seems to come up over and over again is ways of doing this with the compiler compile-time and now recently just marking HashOfString as constexpr and “trust the compiler”. The solution I usually fall back to is to just have a text-file where each line is hashed with a custom script and written to a .h file with values such as:

Read More

The command-line as a poor mans config files

I like command-line arguments as mentioned in an earlier post about them. In this post I’ll discuss a method to use them as simple config-files.

Let’s start of with a usage example from my own code. I have a meshviewer/particleviewer that is used for, you guessed it, viewing meshes and particle-effects. These kind of resources, at least the particle-effects, have internal paths to resources that need to be read while loading ( particles have a material to be rendered with etc ), i.e. resources from “some game” need to be found by the particle-viewer. Since reading resources is done via a VFS ( Virtual File System ) and paths is always specified via this VFS in resources we must just make sure that “some game”:s resources is mounted in the particle-viewer!

Read More