Consider the definitions below.
char right_string[]="::right_one.";
char wrong_string[]="::wrong_one.";
template<const char* str>
void f(){
static_assert(str==::right_string, "Pass me ::right_string!");
}
struct Test{
static constexpr char right_string[]="template_struct::right_one";
static constexpr char wrong_string[]="template_struct::wrong_one";
template<const char* str>
static void f(){
static_assert(str==right_string, "Pass me template_struct::right_string!");
}
};
int main(){
f< ::right_string>(); //compiles, as expected
f< ::wrong_string>(); //does not compile, as expected
Test::f<Test::right_string>(); //compiles, as expected
Test::f<Test::wrong_string>(); //error in Test::f: non-constant condition for static assertion
}
The complete error is
../main.cpp:16:3: error: non-constant condition for static assertion
../main.cpp:16:3: error: ‘(((const char*)(& Test::wrong_string)) == ((const char*)(& Test::right_string)))’ is not a constant expression
I believe that this is a compiler bug, because it doesn't make sense that the constexpr
ness of the expression within static_assert
changes according to what I pass as a template parameter (whether Test::right_string
or Test::right_string
).
I have already found that g++ 4.6 is somewhat flawed when handling addresses as template parameters. Is this an instance of the same bug?
It's a g++ bug, fixed (at least) in 4.7.
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