调试断言在发布版本中将做什么?

Jak*_*ake 4 c++

当处理中文字符(宽)时,我的应用程序在发行版本中表现得很奇怪。我在调试模式下抛出调试断言的行如下:

str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
Run Code Online (Sandbox Code Playgroud)

(其中str是std :: wstring)在调试模式下,此行引发断言。我知道这是因为isspace无法处理宽字符。代替isspace,我必须使用iswspace

str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());

            if (!str.empty())
            { // add str to GUI }
Run Code Online (Sandbox Code Playgroud)

如果我在调试断言中按“ 忽略 ”,则将str正确添加到GUI。但是在发布模式下,str未添加到GUI。

但是,如果我使用iswspace,则将str正确添加到GUI中,而无需对添加到GUI逻辑进行任何更改。

更奇怪的是,在发布模式期间,某些汉字也已正确添加到GUI中。例如,当str为L“?”时,会将str添加到GUI 。但是当它为L“时,是否未添加到GUI

有人遇到过这个问题吗?

我的理解是处于发布模式,因此不会考虑调试断言,并且其工作方式类似于“ 忽略 ”。

编辑:

我进一步调试了它(在发行版中)。看起来在L“情况下if(!str.empty())不会进入内部吗?” 。但是Visual Studio调试器仍然显示L“?在if条件下达到断点时在str内部。

编辑2:我std::locale::global(std::locale(""));在str.erase行上方添加了内容。现在,它在调试和发布情况下的工作原理完全相同,并且文本已添加到GUI。

这是一个例子:

#include <string>
#include <iostream>
#include <algorithm>

int main(int argc, char* argv[])
{
    std::wstring str1 = L"?";
    std::wstring str2 = L"?";
    str1.erase(std::remove_if(str1.begin(), str1.end(), isspace), str1.end());
    if (!str1.empty())
    {
        std::wcout << L"str1 not empty\n";
    }
    str2.erase(std::remove_if(str2.begin(), str2.end(), isspace), str2.end());
    if (!str2.empty())
    {
        std::wcout << L"str2 not empty\n";
    }
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此打印仅“ str1不为空”。

mas*_*oud 5

assert在释放模式下不执行任何操作,将被忽略。

如果NDEBUG在包括其中在所述源代码中的点定义为宏名称,然后断言不执行任何操作12

但是,仅忽略断言并不能解决问题。使您的代码在调试模式下工作。