gpt4 book ai didi

c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?

转载 作者:可可西里 更新时间:2023-11-01 17:58:52 31 4
gpt4 key购买 nike

考虑以下定义。

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 中表达式的 constexprness 根据我作为模板参数(Test::right_stringTest::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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com