简体   繁体   中英

How to set a conditional breakpoint for C++ strings in VS2019?

I'm trying to set a conditional breakpoint for my debug but VS keeps returning me this error:

How can it be so if the operator != is defined for strings?

The variable error is std::string

There are many workarounds you can do, for example a much better way of writing your condition is this:

!errors.empty()

You also have size() that you can compare against 0, c_str() that returns a C string which you can test the first element against \0 , etc etc.

As to the reason why your line doesn't work is that most likely the conditional debugger can't resolve the overloaded operator. Perhaps checking against a non-implicitly built string (ie, error != string{} ) would work better, or using a newer VS version, but really you can see how wasteful that is instead of just simply checking the empty() function result.

Temporarily adding something like this to your code might do it:

const auto s = string_i_want_to_test_to_see_if_its_empty.c_str ();

and then for the conditional breakpoint, the expression would be:

s [0] != 0;

Put that breakpoint on the line immediately after the one added. Hopefully, in a Debug build, it won't get optimised out. If it does, I'm sure you'll think of a way round it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM