I have a class Foo in a namespace that I would like to have a member variable conditionally defined based on a condition. In C, I would have expressed this using an ifdef, but defines suffer from not being namespace-able. Is there a way to achieve the same effect using constexpr?
The closest I've gotten to is the below, but I'd like to have it such that not having is_field_a_present defined results in Bar::a not being instantiated.
In Foo.h:
#include <config.h>
namespace Foo {
struct Bar {
struct empty {};
[[no_unique_address]] std::conditional<is_field_a_present, int32_t, empty>::type a;
};
}
In config.h:
namespace Foo {
constexpr bool is_field_a_present = false;
}