Lorenzo Pistone
Lorenzo Pistone

Reputation: 5188

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?

Upvotes: 8

Views: 1470

Answers (1)

Lorenzo Pistone
Lorenzo Pistone

Reputation: 5188

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

Upvotes: 2

Related Questions