samedi 12 août 2017

Define a static variable in header only using macro

My goal is to be able to create a static variables solely in header with a macro that would take care of initializing it in a .cpp file for me with value I've provided. It should look something like this:

struct UserDefaults {
    STATIC(bool, isFullscreen, true)
    STATIC(bool, isBorderless, false)
    STATIC(std::string, profileName, "") 
}

Which would be equal to:

// .hpp file
struct UserDefaults {
    static bool isFullscreen;
    static bool isBorderless;
    static std::string profileName;
}

// .cpp file
bool UserDefaults::isFullscreen = true;
bool UserDefaults::isBorderless= false;
std::string UserDefaults::profileName = "";

I've looked at this question and pesche's answer but I wasn't able to apply his solution for my case.

Aucun commentaire:

Enregistrer un commentaire