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

标签 c++ templates c++11 constexpr static-assert

考虑以下定义。

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/

相关文章:

c++ - '运营商新' : function does not take 2 arguments

c++ - OpenCV 未定义引用(带 vector 的 FileStorage<KeyPoint>)

c++ - 在 C++ 中是否可以指定使用哪个删除运算符?

c++ - 模板元编程示例没有意义

c++ - 部分模板特化和 icc

c++ - # 包含在 xcode 中找不到的 <cstddef> 文件

c++ - 在模板中推断子类模板类型

c++ - 为什么默认参数不能依赖于非默认参数?

c++ - 根据用户配置在运行时链接共享对象

c++ - cpp 文件中的部分特化不是 "well-formed"