Say I have a base class with a flag inside of it which derived classes have to set:
struct Base
{
bool flag;
Base(bool flag):flag(flag) {}
};
I want to configure which derived classes set the flag to true/false in a data-driven way – i.e. I’d like to configure this from a header.
struct Derived1 : Base
{
Derived1() : Base( expr ) {}
};
Where expr is something (don’t know what yet) that is able to get the info from the header – tell whether Derived1 should make flag true or false. Ideally, I’d get an error if I make a new derived class but fail to specify the flag in the header, but this isn’t mandatory. This way I can just modify a single central location to make changes.
What’s the idiomatic approach for this?
An alternative version that uses a single function might be more compact:
Then in the header:
If you are married to the compile-time check, the best you could do is to introduce a bit of duplication:
This basically relies on SFINAE – the compiler would not be able to find an overload for
theFlagif you called it with an unsupported argument, essentially.