How to see the contents of std::map in Visual C++ .NET (Visual Studio 2003) while debugging? - c++

I need to see the contents of a std::map variable while debugging. However, if i click on it in the Autos/Locals Tab, I see implementation specific stuff, instead of the keys and its contents which I want to look at. Is there a work-around i'm missing ?

I don't have a copy of Visual Studio 2003 around to test with, but I just checked VS2008 and 2010 and both automatically display the contents of the map on hover. I vaguely remember having a lot of trouble with STL inspection in 2003, so my guess is that it's a feature that they substantially improved in the intervening years.
If it's possible for your project, you might consider upgrading to Visual C++ 2008 Express, which is free and should have most of the features you need.

I have no VS2003 nearby at the moment. But you could try to add in "autoexp.dat" the following section (I was sure that in VS2003 there are already included sections for all standard types):
;------------------------------------------------------------------------------
; std::map
;------------------------------------------------------------------------------
std::map<*>{
children
(
#tree
(
head : $c._Myhead->_Parent,
skip : $c._Myhead,
size : $c._Mysize,
left : _Left,
right : _Right
) : $e._Myval
)
preview
(
#(
"[",
$e._Mysize,
"](",
#tree
(
head : $c._Myhead->_Parent,
skip : $c._Myhead,
size : $c._Mysize,
left : _Left,
right : _Right
) : $e._Myval,
")"
)
)
}
The structure of this file and the syntax of autoexp rules may change from one release of Visual Studio to the next. Read here more about custom visualizers for Visual Studio.

Related

How to stop visual studio 2017 c++ from autocompleting ) while in the middle of FOR statement?

So every time I write something like
for (auto i = 0; i < my_vector.size();
the moment I add the semicolon after a function call in a for statement, it turns into
for (auto i = 0; i < my_vector.size());
and then I have to manually delete the automatic bracket close because I havent finished the for statement, super annoying.
I don't remember earlier VS versions doing this.
Anyway I tried going in options -> text editor -> c/c++ -> formatting -> general, I tried switching off "Automatically format statement when I type a ;" and also "Automatically format braces when they are automatically completed". Both had some minor effect but the problem I described was still happening.
I like autocomplete most of the time, I find it useful so I don't want to turn it off completely but how can I make it stop doing that one thing?
Update the Visual Studio. That particular (VS 2017) bug is now fixed starting with the VS 2017 15.4 Preview 2 update.

What does the code does and how can i use it in my custom projects?

I fount the below code in QT header file named "mywidget.h".
Could any one please clarify the below questions:
What the code does ?
Is it a good practice ?
Where can I find similar kind of code snippets to enhance my knowledge?
#ifndef %PRE_DEF%
#define %PRE_DEF%
#include <%BASECLASS%>
class %CLASS% : public %BASECLASS%
{
Q_OBJECT
public:
%CLASS%(QWidget *parent = 0);
~%CLASS%();
};
#endif // %PRE_DEF%
This code is just a code snippet used to create new class.
What it does?
It just inserts this code snippet into your text/source file and selects ( or marks ) all `%NAME% keywords which let's you change it to desired name.
Is it a good practice?
It depends. If you're tired of typing new classes and you know how to do this then answer is yes it is. It let's you create object definitions in less steps.
Where can I find similar kind of code snippets to enhance my knowledge?
This depends on IDE you're using. With Visual Studio you can add these in :
C:\Users\your_username\Documents\Visual Studio version\Code Snippets
There you'll find different folders named like Visual C++ in which you can add them.
EDIT:
Answering to the comment:
You dont have to use any kind of IDE, you can just copy the content by hand and modify it. But ( just for clarity ) it's easier to use IDE instead of hand copying and changing the content.
Complete explanation on how to use code snippets:
Qt Creator
Visual Studio

Switch autocompletion for enum in Visual Assist or Vanilla Visual Studio

Is there anyway to have a switch expand with all the values of an enum using Visual Assist or Vanilla Visual Studio?. The included snipsets just insert a basic switch with just a default entry. I'm using last visual studio 2013 update.
VS 2013 can build a switch statement for an enum using a default snippet. (Note: use a default VS snippet, not a VA Snippet.)
As you type "swit", accept the suggested code snippet from VS. Replace "switch_on" with your enum and press Enter. Your statement will expand. (You need two Enters if one selected your enum from a listbox.)
After the switch has initially been generated, if you add new enum values, then you can use the Add Missing Case Statements command in Visual Assist to populate the switch statement with the new values (shift+alt+q, m).
In Visual Studio 2015, the auto-completion of switch cases is defeated if the discriminator is declared with const as in this example:
auto const enu = static_cast<MediaType>(discriminant);
Remove const, and voila--it works again!
(Then you can put the const back in, if you wish.)

VARIANT members not resolved in C++ OLE automation application (Eclipse IDE)

I'm developing a C++ application that reads and writes to Excel using OLE automation (code based on this: http://support.microsoft.com/kb/216686).
The application has been up and running just fine when I was using VS2010. However recently I decided to move to Eclipse, and all of a sudden the compiler (CL) won't recognize the structure of the VARIANT struct (doc: http://msdn.microsoft.com/en-us/library/aa908601.aspx). For instance in this piece of code:
VARIANT tmp;
tmp.vt = VT_R4;
tmp.fltVal = 5.0f;
tmp.fltVal cannot be resolved in Eclipse (while it works just fine in VS2010, and every code sample I've seen online). The only way to get Eclipse to stop displaying an error is to replace the last two lines with:
tmp.n1.n2.vt = VT_R4;
tmp.n1.n2.n3.fltVal = 5.0f;
Same goes for tmp.parray and all other members of VARIANT.
Has anyone come across this before ? What am I missing here ?
PS: No, replacing the code everywhere is not exactly an option

How do I write a very simple Visual Studio debugger visualizer?

I'm trying to write an 'autoexp.dat'-based visualizer for a string type. I've scaled-back my ambitions to attempting to write a visualizer for a really simple test type that contains a null-terminated string field:
namespace thizz { namespace izz {
class MyType {
const char* _ptr;
public:
MyType(const char* ptr) : _ptr(ptr) {}
};
}
}
This is my stab at a visualiser, but it has no effect on how Visual Studio (2010) displays an instance of this type:
thizz::izz::MyType
{
preview ([$e._ptr,s])
}
(That's going at the top of the [Visualizers] section in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\Debugger\autoexp.dat).
Watching an instance of this type:
thizz::izz::MyType t("testing testing");
Just displays
t | {_ptr=0x0f56a6fc "testing testing" } | thizz::izz::MyType
in the Watch window.
To get an even more versatile viewer try changing to use this:
thizz::izz::MyType {
preview ( #( [$e._ptr,s] ) )
stringview ( #( [$e._ptr,sb] ) )
}
this will also give the magnifying glass icon which will open a larger text view window in the case that you have a longer string. It'll also give you the option of rendering as HTML or XML.
Note that as well as the format of the file being sensitive to whitespace, I've also found that you can't use a colon in the string otherwise it generates parse errors.
The debugger visualisers are incredibly powerful, though the syntax can be quite bewildering. As general advice I would suggest creating some entries first in the [AutoExpand] section to summarise the data types that you are most interested in, and then if you have custom containers then copy and adapt the examples for vector, list, etc, which will give you the largest return for the investment in your time.
I can't give a categorical reason why my original 'code' in autoexp.dat was not working, but I found that the same code worked when all the whitespace was removed.
I then tried re-adding whitespace and found that keeping the initial open brace on the first line was necessary to keep the definition working.