Switch autocompletion for enum in Visual Assist or Vanilla Visual Studio - c++

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.)

Related

defaulting to using braces enclosing each case in switch statements in Visual Studio Code

In C++ or C#, it's generally a good practice to enclose each case within curly braces (e.g., see C# switch statement with curly braces for each case/default block within the switch statement?).
But Visual Studio Code defaults to creating a template that leaves them out.
What UI preferences can I change so that they are included by default?
Edit: I am not interested in a debate about whether adding curly braces should always be done or not, but rather knowing how to change VS Code's UI for this context.
You should add a snippet by yourself.
Select Command palette (F1) -> Preferences: Configure User Snippets -> C++ and add the following code.
"switch2": {
"prefix": "switch2",
"body": "switch (${1:expression}) {\n\tcase ${2:/* constant-expression */}: {\n\t\t${3:/* code */}\n\t\tbreak;\n\t}\n\tdefault: {\n\t\tbreak;\n\t}\n}"
}
Ended up slightly modifying #umitu's answer. Posting it here just in case someone else finds it useful as well:
"switch2": {
"prefix": "switch2",
"body": "switch (${1:expression}) {\ncase ${2:/* constant-expression */}: {\n\t${3:/* code */}\n\t} break;\ncase ${4:/* constant-expression */}: {\n\t${5:/* code */}\n\t} break;\ndefault: {\n\t${6:/* code */}\n\t} break;\n}"
}
And now the default snippet looks like the following:

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.

Google gflags library in visual studio: Can't inspect FLAGS_ variables

I have built google gflags in windows using visual studio 2015.
The built is a Debug build (so Program Database (/Zi) is set) and I'm stepping through the gflags_unittest_main.cc.
The unit test passes successfully. However, while debugging I can't see the contents of the FLAGS_## variables.
For example, line 1515 of gflags_unittest_main.cc is
FLAGS_changed_bool2 = true;
I can't see the value of FLAGS_changed_bool2 in the watch window. I have tried adding the google:: and gflags:: namespaces in front of FLAGS_changed_bool2 but it just says identifier is undefined. Here it says you can access these flags just as a normal variables.
Why can't I see the values of these variables while debugging? What can I do to see them? maybe some build option?
I'm trying to understand the code of another open source library that uses gflags and has hundreds of FLAGS_ variables. Not being able to see the contents of these variables makes the task more difficult.
I found a workaround for this. Each of the FLAGS_ variables are created inside namespaces which include a short identifier for the type (see DEFINE_VARIABLE macro in gflags.h). For example, fLI for integer, fLB for bool, fLS for String, etc.
So if I add to the watch window the following:
fLB::FLAGS_changed_bool2
fLI64::FLAGS_this_is_an_int64_variable
I can see their values.
The DEFINE_VARIABLE macro adds a using statement
using fL##shorttype::FLAGS_##name
after the definition of the variable inside the fL##shorttype, which allows the user code to refer to FLAGS_##name directly. However the Visual Studio debugger ignores this using and needs the namespace for each of these variables. I guess the next question is how to get visual studio to use this using statements, so that I can hover my mouse pointer over the variable a see its value.

Visual Studio Auto Format Curly Brace on Next Line

I am so used to code like this:
if (...)
{
//somecode
}
that I simply have to change the following to the above:
if (...){
//somecode
}
I get a lot of code from other people that I have to go through and sometimes this is what I get. Is it possible to auto-format that in Visual Studio 2008 for C++?
Now some may think I should get used to it. Believe me, I am trying, but it is very distracting and I hope there is a simple solution.
VS2008 has a simple formatter (Edit menu, Advanced, Format Selection), but it doesn't move braces.
Check out astyle and its --style=ansi flag.

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

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.