c++条件断点设置string比较

无效情况说明

c++不支持条件断点直接判断std::string为某值:str=="123"。

但是支持一些符号的调用:

“Visual Studio中的添加断点功能中支持的字符串函数有:strlen, wcslen, strnlen, wcsnlen, strcmp, wcscmp, _stricmp, _wcsicmp, strncmp, wcsncmp, _strnicmp, _wcsnicmp, strchr, wcschr, strstr, wcsstr.”

但是,也不能用这样的条件:strcmp(str.c_str(), "123") == 0。会提示函数调用(c_str())会有副作用,所以不计算值。但是str.size() == 5这样的条件可以,难道因为size()直接返回值吗?

 

有效方法

调查了一下,只能通过访问成员的方式进行比较。可以通过watch窗口查看string有什么成员:

输入“str,!”,逐级查看:

在条件断点中输入以下一个条件(按需领取):

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0


(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0

 主要参考:visual studio - How to create conditional breakpoint with std::string - Stack Overflow

下面Tomas Tintera的评论:

 

 关于QString,没有找到方法