8

考虑下面的定义。

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:错误:静态断言的非常量条件

../main.cpp:16:3: 错误:'(((const char*)(& Test::wrong_string)) == ((const char*)(& Test::right_string)))' 不是常量表达式

我相信这是一个编译器错误,因为根据我作为模板参数传递的内容(无论是还是)constexpr,表达式的内在变化是没有意义的。static_assertTest::right_stringTest::right_string

我已经发现 g++ 4.6在将地址作为模板参数处理时有些缺陷。这是同一错误的实例吗?

4

1 回答 1

2

这是一个 g++ 错误,(至少)在 4.7 中修复。

于 2012-05-02T17:14:37.777 回答