Visual Studio 2008 Enum Bit Flags Visualization

Visual Studio 2008 Enum Bit Flags Visualization

  • Comments 17

In Visual Studio 2008 we introduced a useful, but not very well known, feature in the Native Debugger for visualizing enum bit flags. Consider the following code:

enum x {

bit_none = 0,

      bit1 = 1,

      bit2 = (1 << 1),

      bit3 = (1 << 2),

      bit4 = (1 << 3),

bit_all = -1

};

Let’s define a variable:

x flags = static_cast<x>( bit1 | bit2 | bit4 );

In Visual Studio 2005, the Debugger shows the following in a Locals Windows:           

clip_image002

But Visual Studio 2008 is better than that.  Instead, you’ll get this:

clip_image004

which is more intuitive and readable as we see field names instead of integer values.

This feature was specifically designed to visualize bit flag names that are a power of two. As soon as an enum contains members whose values don’t adhere to this requirement, the visualization won’t work since arbitrary numeric values (i.e. those not a power of 2) cannot be interpreted by the debugger in a single way. This rule has two exceptions: we support a value of 0, which indicates no bits set, and a value with all bits set for the underlying type.  In the case of int this value is -1 (yes, it is platform specific J).

The Debugger is the single most used feature of Visual Studio. We are well aware of that and are committed to constant improvements in this area. We are always interested in hearing your ideas for what you’d like to see in the next version of the Visual C++ Debugger.  Let us know if you have any interesting requests!

Thanks,

Vytautas Leonavičius/Visual C++

  • PingBack from http://msdnrss.thecoderblogs.com/2008/03/10/visual-studio-2008-enum-bit-flags-visualization/

  • x flags = static_cast<x>( bit1 | bit2 | bit4 );

    I don't think that's legal C++.

  • To Bruno Martinez:  Why not?  It looks to me like 5.2.9 (page 75 first paragraph) addresses exactly that.

    To Vytautas Leonavičius:

    I wish that the default display of values would be hexadecimal for DWORD, WORD, BYTE, pointers, etc., and decimal for UINT, int, long, etc.  With enums the debugger would have to guess the intended usage, but in your example you could guess.

  • Nice.

    I agree that the debugger is the single most used feature of Visual Studio. I could say VC's debugger is the only thing that keep me using MS Windows...

    Speaking of feature request, is it possible to make autoexp using a scripting language or a dll? In other words, make visualization programmable. The autoexp used now is too weak IMO. If the visualization process is programmable, we could add such enum display trick ourself.

  • If you are using template classes, even standard one from stl (vector, map), it is almost impossible to see function name in stack trace.

    Is it possible to collapse template parameters in type of some template ? Thus, instead of:

    std::vector< std::list<int, std::standard_allocator>, ...>::foo<... bla bla>

    we should see only:

    std::vector<>::foo<>

    in stack trace with optional '+' sign near angled brackets expandable on click.

    If it is to hard, I would be thankful for option "hide template parameters in watch windows", because, most of the times, I don't need that, space consuming, info.

  • I would love it if you could finally call methods that overloaded with both a standard and const version from the watch window.

  • Please support "operator overloading" in watch window, in particular [] and -> , since these are most important when using Smart Pointers and smart arrays!

  • Yeah, debugger is very nice in 2008.

    I'm also very pleased to see a Lithuanian developer in Visual C++ team! :)

    Go Vytautas! ;)

  • Nice tip! Thanks V. And interesting style of enum declaration - in all my years, I haven't seen it done that way.

    John

  • You are right about the legality.

  • I would love it if I could add an expression like this:

    a->b[i]->c[j] to the watch window where b and/or c are of type std::vector

  • Good Job, Vytautas!

    That's a really nice feature.

    Thanks,

    Robert.

  • I agree with Bruno, thats not legal C++ (actually it IS grammatically legal but this shouldn't be done like this). A combination of enum values have to be stored in the underlying type, int for example, because the result is not an "x" anymore...

  • "the result is not an "x" anymore..."

    But the result is still within range.  I guess it's necessary to add section 7.2 paragraph number 6 (page 113 third paragraph) to section 5.2.9 paragraph number 7 (page 75 first paragraph).

  • This does not work if (1<<31) is defined in the enum, not even if the underlying type is __int64!? Thats a shame really, because many enums in our code use this value. What gives?

    And in principle, who would care if its legal c++ when the debugger allows for the expression "(x)flags" in the watch window?

Page 1 of 2 (17 items) 12