Suppose bad_name
is a restricted identifier for example that I do not want to be part of the struct. I am looking for a mechanism to force a compilation failure in that case.
example.h
struct example {
int okay_name;
int bad_name;
}
main.cc
#include "example.h"
int main() {
example ex;
// cause compilation to fail here if bad_name is a member of ex
return 0;
}
There are probably ways to cause a failure at runtime by simulating reflection, but is there a way to do this at compile time?
You can define bad_name
to be something that would cause a compile time error. For example, nothing:
#define bad_name
gives on GCC
error: declaration does not declare anything [-fpermissive]
int bad_name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With