PaulS:
int a;
Local variables are not initialized, unless you provide an initial value.
int baz = foo<__builtin_constant_p(a)>::bar;
Why would you pass an uninitialized variable to any kind of function, constant or not?
To show an minimal example of my problem.
It works in gcc 4.7.1, which I have. I guess the gcc people can implement things given 4 years (gcc 4.3.2 was released August 27, 2008)
Well the intention is to use a different struct from that one: http://arduino.cc/forum/index.php/topic,124974
pYro_65:
Not possible sorry, join my band wagon and encourage Arduino to accept c++11, the constexpr keyword is what you are looking for. I'm pretty sure its just a command line flag change.C++, as a rule instantiates everything at compile time ( its how it can optimise ), templates however cannot be instantiated until parameters are provided. So each specific variation of a template is specific to its parameters. Which means you cannot instantiate a template at runtime as there is no specific parameters compiled for it.
Mutable variables are not considered compile time constants. Even when marked as const ( taking the address can allow changing of the data )
Well I'm no expert in C++11, but when I try to compile the same program with "-std=c++11" or "-std=gnu++11" I get the same errors. Do you mean that __builtin_constant_p() should be declared constexpr? It's a gcc builtin, so it's not a true function.
Also, the same program does compile without optimizations (with __builtin_constant_p(a) returning 0), and this does compile with __builtin_constant_p(a) as 1. So it's not a problem that the return of __builtin_constant_p() is not constant, it's that it's sometimes constant.
template<bool Flag> struct foo {static const bool bar = Flag;};
int main() {
const int a = 4;
int baz = foo<__builtin_constant_p(a)>::bar;
}
I'm thinking I'll report it as a bug in gcc, since there's no reason why __builtin_constant_p() would ever return a non-constant.
If nobody thinks that it's possible to make that program work, my next question is "How can I make a variable one type if a different one is constant and a different one if it's not?"
ie, get a
to be an int
if b
is constant, and a
to be a long
if b
is not constant.