簡體   English   中英

static_assert無法將const char *模板參數識別為constexpr:g ++錯誤?

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

請考慮以下定義。

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)))'不是常數表達

我相信這是一個編譯器錯誤,因為根據我作為模板參數傳遞的內容(無論Test::right_string還是Test::right_string ), static_assert表達式的constexpr性沒有意義。

我已經發現在將地址作為模板參數處理時,g ++ 4.6 有點缺陷 這是同一錯誤的實例嗎?

這是一個g ++錯誤,至少已在4.7中修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM