I am trying to replace the preprocessor #define and #if/#ifdef's that I am using to control conditional compilation with constexpr variables and ifs.
Is it possible to declare constexpr variables such that they reproduce the #defines in that they don't allocate runtime storage, and that taking the address of one leads to a compile time error?
Edited to add code example.
So in the headers I want to have something like
namespace ExampleNamespace
{
enum class Platform : int {Darwin, Linux, Windows};
constexpr Platform BuildPlatform = Platform::Darwin; // Line A.
};
While in the code I want
if constexpr (Platform::Darwin == BuildPlatform) // Line B.
{
cout << "Platform is Darwin" << endl;
}
else
{
cout << "Platform is not Darwin" << endl;
};
const Platform *const PlatformAddress = &BuildPlatform; // Line C.
const Platform &BuildPlatform2 = BuildPlatform; // Line D.
Then aim is then to change the definition of BuildPlatform on Line A, such that Line B is evaluated at compile time (and the else clause gets discarded/not compiled) and lines C and D (and anything that does the same thing, or relies on runtime storage for BuildPlatform) generates a compiler error.
Is such a construct possible in C++17?