2017-10-18 137 views
3

編譯constexpr代碼時編譯以下代碼(godbolt):「ERR:SEH:setup_exception堆棧溢出」 在MSVC

constexpr bool f(const char *&s) { 
    do { 
    ++s; 
    } while (*s); 
    return true; 
} 

constexpr bool g(const char *s) { 
    return f(s); 
} 

int main() { 
    static_assert(g("x"), ""); 
} 

給出MSVC此錯誤:

err:seh:setup_exception stack overflow 1552 bytes in thread 0058 eip 000000007b48dad8 esp 0000000000131000 stack 0x130000-0x131000-0x1130000 

而其他的編譯器( GCC和Clang)高興地接受它。

爲什麼不在MSVC上編譯這個代碼,我該如何解決它?

+0

我能想到的唯一的事實是,在執行空檢查之前,您正在增加指針*。這將在空字符串上調用未定義的行爲。我不知道爲什麼這是一個問題。在Godbolt中更改代碼以檢查增量之前似乎無法解決問題。 – Xirema

+0

很明顯是一個錯誤。即使該代碼無效,這也不是MSVC應該給你的信息。 –

回答

1

我能夠得到它通過從函數簽名參考預選賽(Godbolt)編譯:

constexpr bool f(const char *s) {//No longer passes a pointer by reference 
    while(*s) { //This avoids undefined behavior when the passed string is empty 
    ++s; 
    } 
    return true; 
} 

constexpr bool g(const char *s) { 
    return f(s); 
} 

int main() { 
    static_assert(g("x"), ""); 
} 

我不知道爲什麼通過引用傳遞指針會導致這個錯誤,不過,我認爲這可能是MSVC編譯器中的一個錯誤。你應該將錯誤發佈到他們的bug tracking forum