考虑以下定义。
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
}
完整的错误是
../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
我认为这是一个编译器错误,因为 static_assert
中表达式的 constexpr
ness 根据我作为模板参数(Test::right_string
或 Test::right_string
)。
我已经发现 g++ 4.6 是 somewhat flawed将地址作为模板参数处理时。这是同一个错误的实例吗?
最佳答案
这是一个 g++ 错误,(至少)已在 4.7 中修复。
关于c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10405835/