gpt4 book ai didi

c++ - 调试断言在发布版本中究竟会做什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:37 25 4
gpt4 key购买 nike

我的应用程序在处理中文字符(宽)时在发布版本中表现异常。我有下面一行在 Debug模式下抛出调试断言:

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

(其中 str 是 std::wstring)此行在 Debug模式下抛出断言。我知道这是因为 isspace 无法处理宽字符。我必须使用 iswspace 而不是 isspace

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

if (!str.empty())
{ // add str to GUI }

如果我在调试断言中按“忽略”,str 会正确添加到 GUI。但在 Release模式下,str 不会添加到 GUI。

但如果我使用 iswspacestr 会正确添加到 GUI,而不会对我添加到 GUI 的逻辑进行任何更改。

更奇怪的是,在 Release模式下,一些中文字符也被正确地添加到 GUI 中。例如,当str为L"左"时,它被添加到GUI中。但在L"右"时不添加到GUI。

有人遇到过这个问题吗?

我的理解是在 Release模式下,将不考虑调试断言,其工作方式类似于“忽略”。

编辑:

我进一步调试它(在发布中)。在 L"右" 的情况下,它似乎不会进入 if(!str.empty()) 内部。但是当它在 if 条件下遇到断点时,visual studio 调试器仍然在 str 中显示 L"右"。

编辑 2:我在 str.erase 行上方添加了 std::locale::global(std::locale(""));。现在,它在调试和发布情况下的工作方式完全相同,并且文本已添加到 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;
}

这只打印“str1 not empty”。

最佳答案

assert 在 Release模式下什么都不做,将被忽略。

If NDEBUG is defined as a macro name at the point in the source code where is included, then assert does nothing 1,2.

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

关于c++ - 调试断言在发布版本中究竟会做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com