c++ - 条件断点 : This expression has side effects and will not be evaluated

标签 c++ debugging visual-studio-2013

我有一个名为 size_t A::m() const 的非静态常量方法,如果它返回的值大于 1,我想用它来触发断点。这是 A 类 和实例 a:

class A
{
public:
    std::vector<double> myvec;
    size_t m() const
    {
      return myvec.size();
    }
} a;

所以我在 Visual Studio 2013 中添加了一个断点,这个条件

a.m() > 1 // a is an instance of class A

但是,当我尝试编译它时,我从 IDE 收到以下消息:

The following breakpoint cannot be set:

At myFile.cpp, line xxx, when 'a.m() > 1' is true

This expression has side effects and will not be evaluated.

请注意,A::m() 不会修改任何内容,它只会调用 vector 的 .size() 方法并返回该值,因此断言表达式有副作用是完全错误的。事实上,替换断点条件为a.myvec.size() > 1(即方法本身的内容)具有相同的效果!

关于什么可以用作断点中的条件,Microsoft says that ;

The condition can be any valid expression that is recognized by the debugger.

所以我去看了Expressions in the Debugger , 和 found this :

One common cause of side effects is evaluating a function call in a debugger window. Such evaluations are usually noticeable. A more subtle cause of side effects is the evaluation of properties and other implicit function calls in managed code.

The debugger cannot tell whether a property evaluation or implicit function call has side effects. Therefore, by default, the debugger does not evaluate implicit function calls automatically. Property evaluation is allowed by default, but can be turned off in the Options dialog box. When a function call or property has not been evaluated, a refresh icon appears. You can manually evaluate the expression by clicking the refresh icon. For details, see How to: Refresh Watch Values.

When evaluation of properties or implicit function calls is turned off, you can force evaluation by using the ac format modifier (for C# only). See Format Specifiers in C#.

如果有人能将上面的段落翻译成英文那就太好了。我能否将函数放入这些调试器条件语句中?

最佳答案

这是我对您提供的帮助链接的翻译:

  • 第 1 段:调用函数可能有副作用。评估属性可能会产生副作用。
  • 第 2 段:调试器无法判断是否存在副作用,因此我们只是假设:“函数 = 坏”、“属性 = 好”(即“函数有副作用,属性没有” ).刷新与当前问题无关的图标信息。
  • 第 3 段:想要强制调试器?如果您使用的是 C#,请在求值后放置 ,ac

所以,归结起来就是,如果你想在你的评估中调用一个函数并且正在使用 C#,在它后面加上 ,ac

a->m() > 1,ac

由于您使用的是 C++,我认为这可以归结为“您的评估语句中没有函数!”出于调试的目的,您可能可以从 A::m 中删除常量,因为说明符不会(不应该)对逻辑流产生任何影响.不过,我什至不确定这是否可行。

关于c++ - 条件断点 : This expression has side effects and will not be evaluated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19778948/

相关文章:

c++ - C++中 "~"(波浪号)符号的含义?

web-services - 如何让 Visual Studio 自动将其调试器附加到 Web 应用程序和 Web 服务?

android - Facebook 连接不工作(SDK 3)

c - 调试缓冲区溢出引起的段错误

c++ - 使用 Cmake 和 Visual Studio 2013 Unresolved external 问题

c++ - 进程间内存编辑-查找更改的地址

c++ - 标准 12.1 p14 中的 "unspecified value"是什么意思?

visual-studio - 彻底删除Visual Studio网站解决方案

c++ - 如何在 vscode 中链接并包含来自不同目录的 C++ header

windows - 将 Windows Phone 应用程序转换为通用应用程序