Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 constexprness 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?

like image 892
Lorenzo Pistone Avatar asked May 01 '12 23:05

Lorenzo Pistone


1 Answers

It's a g++ bug, fixed (at least) in 4.7.

like image 131
Lorenzo Pistone Avatar answered Sep 21 '22 11:09

Lorenzo Pistone