Suppose that I have a type:
struct Foo {
int a, b, c, d, e;
};
Now, I'd like to have a macro (or any other solution), which can define a Foo object in a way, that if the object can be constexpr (because it is initialized with compile-time ints), then it defines it as static constexpr Foo. If cannot be constexpr, then it defines as const Foo (I'd use this macro in function scope).
So, I'd like to have a macro (or some equivalent solution):
#define DEF(a, b, c, d, e) ... // magic here
If I call it with compile-time constants:
DEF(1, 2, 3, 4, 5);
Then I'd like this to expand to:
static constexpr Foo foo{1, 2, 3, 4, 5};
But if any of the parameters are not compile-time constants (so it cannot be constexpr):
int b = 2;
DEF(1, b, 3, 4, 5); // second parameter is not a compile-time constant
then I'd like to have:
const Foo foo{1, b, 3, 4, 5};
The reason that I'd like to have something like this is that compilers are not allowed to optimize away foo from stack, so I have to do this optimization manually.
(Note, that I use Foo a lot of places, that's why I'd like to have an automatic solution. Currently I need to decide whether foo should be static or not case-by-case, which is tedious.)