简体   繁体   中英

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. I'm wondering what the syntax would be to get at that variable.

Here is a simplified version of the C++ code:

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). Here is the visualizer Type, with <what goes here?> at the place where I'm having trouble:

<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. As there is no symbol path to that object when you are not within the function, you're 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.

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