Search code examples
c++visual-c++boostvisual-studio-debuggingdebuggervisualizer

Howto create (VC14) debug visualiser for string_view?


I’m using boost::string_view. (www.boost.org/doc/libs/1_61_0/boost/utility/string_view.hpp)

Its got a const char* ptr_ and a unsigned int len_ data member. When debugging its cumbersome, as the default visualiser will show the char ptr_ with more charachters than length (as string_view is not null terminated).

I tried creating my own visualiser by looking at the std::string visualiser. I can get the string to be displayed if I hard code the length (to say 4), but I cant get it to use the actual length variable. Any help appreciated. Broken example:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="boost::basic_string_view&lt;char,*&gt;" Priority="High">
    <DisplayString>{ptr_,4}</DisplayString> //works...
    <DisplayString>{ptr_,len_}</DisplayString> //doesnt work...
  </Type>
</AutoVisualizer>

Solution

  • After spending a whole day of trial and error, I found the frustratingly simple solution. It should be

    "{ptr_,[len_]}" 
    

    It's documented here:

    https://msdn.microsoft.com/en-us/library/windows/hardware/dn936815(v=vs.85).aspx

    My working example:

    <?xml version="1.0" encoding="utf-8"?>
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
      <Type Name="boost::basic_string_view&lt;char,*&gt;" Priority="High">
        <DisplayString>{ptr_,[len_]s8}</DisplayString>
        <Expand>
          <Item Name="[size]" ExcludeView="simple">len_</Item>
          <ArrayItems>
            <Size>len_</Size>
            <ValuePointer>ptr_</ValuePointer>
          </ArrayItems>
        </Expand>
      </Type>
    </AutoVisualizer>