2

I have a type which represents "Battleship" coordinates:

struct BattleshipCoordinates
{
    int row; // zero-based row offset
    int col; // zero-based column offset
}

Note that the coordinates are stored natively as zero-based index offsets. I would like to display these in the debugger in a more 'natural' view for Battleship coordinates (i.e. when the structure contains {0, 0} I would like the debugger to display "A1" for the upper left corner). I would like to accomplish this custom formatting with a .natvis file.

I am able to translate the values to their respective chars ('A' and '1'), but the debugger displays them in an offputting format with extra formatting:

<Type Name="BattleshipCoordinates">
  <DisplayString>{(char)(row + 'A')}{(char)(col + '1')}</DisplayString>
</Type>

There are a number of issues with this approach; the current result for {0,0} is displayed in the debugger as 65'A'49'1'. I would like to remove the extra formatting (numbers and quotation marks) and just display simply "A1". Additionally, that syntax would break as soon as the column reaches double-digit values.

What secret sauce am I missing? Is there some method through which I can stream together multiple values?

If I could access stringstreams, I could just use: ostr << static_cast<char>(row + 'A') << (col + 1). If I could call one of the available to_string functions in my code, that would also work; but to my knowledge, none of that is available in natvis syntax...

BTownTKD
  • 7,911
  • 2
  • 31
  • 47
  • I don't think this is possible. You can define [format specifiers](https://learn.microsoft.com/en-us/visualstudio/debugger/format-specifiers-in-cpp?view=vs-2022#BKMK_Visual_Studio_2012_format_specifiers) for a code snippet, but you can't change how those specifiers are displayed. In your example the character format specifier is used because of the cast. It's the same as `{(row + 'A'),c}`. It's just another way of displaying a number. `{(row + 'A'),d}{(col + '0'),d}` would display `6548`. But there is no format specifier for displaying the character only. – local-ninja Jul 05 '23 at 00:56

0 Answers0