简体   繁体   中英

static_assert doesn't recognize a const char* template parameter as constexpr: g++ bug?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM