简体   繁体   English

Visual Studio 2015 Natvis如何显示函数静态变量?

[英]How can Visual Studio 2015 Natvis display a function static variable?

I am writing debugger visualizers using a .natvis file in Microsoft Visual Studio 2015. There is one piece of information in my class that I would like to get at if possible. 我正在使用Microsoft Visual Studio 2015中的.natvis文件编写调试器可视化工具。如果可能的话,我想在课程中提供一条信息。 I'm wondering what the syntax would be to get at that variable. 我想知道获取该变量的语法是什么。

Here is a simplified version of the C++ code: 这是C ++代码的简化版本:

class MyClass
{
public:

    MyClass() {}

    int getAValue(size_t index)
    {
        static std::vector<int> numberVector;

        if (numberVector.size() <= index)
        {
            addSomeNumbersToTheEnd(numberVector);
        }

        return numberVector[i];
    }
}

In the debugger, I would like to see the size of the vector when I hover over an instance of MyClass, but I don't know how to reference it (or if that is even possible). 在调试器中,当我将鼠标悬停在MyClass的实例上时,我希望看到向量的大小,但我不知道如何引用它(或者如果可能的话)。 Here is the visualizer Type, with <what goes here?> at the place where I'm having trouble: 这是可视化器类型,在我遇到问题的地方有<what goes here?>

<Type Name="MyClass">
    <DisplayString>[$(Type)] staticVectorSize={<what goes here?>}</DisplayString>
</Type>

The actual problem is much more complicated, involving the curiously recurring template pattern to create better enumeration objects, so please no comments about the uselessness of this somewhat contrived scenario. 实际问题要复杂得多,包括奇怪的重复模板模式以创建更好的枚举对象,所以请不要评论这个有点人为的场景的无用性。

If you can get your watch window to state the static function variable value outside of the function, then you can use that. 如果您可以让您的监视窗口在函数外部声明静态函数变量值,那么您可以使用它。 However, AFAIK, access to a static function variables are only allowed inside of the function scope. 但是,在AFAIK中,只允许在函数范围内访问静态函数变量。 As there is no symbol path to that object when you are not within the function, you're SOL. 由于当你不在函数内时没有到该对象的符号路径,你就是SOL。

A workaround is to move the static variable to the class scope, then there is a symbolic path to the variable, and you can access it from there. 解决方法是将静态变量移动到类范围,然后有一个指向该变量的符号路径,您可以从那里访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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