visual studio intellisense for c++ - c++

I am using visual studio and In the following c++ code
using namespace std;
void f() {
}
int main()
{
// using ctrl + space
cin.get();
return 0;
}
When I use Intellisense suggestions in main function it does suggest f function but when I click on it or press tab it just writes f not f().
Is there anyway to correct this and not to add () myself after all function calls?

It is a well-known problem that I have handled it before.
For VS, actually, it could not be done automatically now and VS does not have this Intellisense feature by default.
As a suggestion, before you click Enter to complete automatic filling from the Intellisense suggestion window, press the key combination Shift + ( and it will automatically add () with the method.
Or just Use Resharper Extension and this extension has the feature for automatically adding brackets to methods.
Besides, this DC ticket has raised this idea to the Team. You could vote it and more votes will help the Team pay more attention to this and add this feature to VS IDE as soon as possible.

Resharper is the extension that does just that
https://www.jetbrains.com/resharper-cpp/

Related

Visual Studio 2019 - don't insert a linebreak when opening curly braces (C++)

I want to declare a variable like this:
int foo {5};
But as soon as I open the first curly brace, VS does this instead
int foo {
}
I've spent a long time trying to find the relevant option but haven't had any luck.
Thanks.
Click "Options" in the upper toolbar, go to "text editor" in the left menu in the appeared window, choose "C++", "Formatting" and order Visual Studio to never newline such statements.
I couldn't reproduce the issue at vs2019. I suggest you could try to reset settings.
Tools->Import and Export Settings...->Reset all Settings->Next->"No, just reset settings, overwriting my current settings"->Next->General->Finish

Indenting function braces in Eclipse

I am working on a C++ project and using Eclipse as the IDE. The coding standards for the project specify indenting the initial and closing braces for a function, as in:
int main()
{
return 0;
}
Is there an option in Eclipse 4.3.1 to automatically indent this way?
Use the main menu to Navigate to Window->Preferences.
On the resulting dialog, expand your way to C/C++->Code Style->Formatter. Set Active Profile to Whitesmiths and edit to taste.
For example, it looks like you want to set Indentation size and Tab size on the Indentation tab to 2.

How to fix visual studio autocomplete when typing for loops?

This has been really bugging me ever since I switched from VS 2015 to VS 2017.
Here's what's happening:
When I type this
for(int i = 0; i<v.size()
then add a semicolon, VS autocompletes the line to this:
for(int i = 0; i<v.size());
This is clearly not what I want, since I still need to type the increment portion of the for loop.
How do I prevent autocomplete for this specifically?
It is strange that you are typing this manually. Have you tried using stock code snippets? Type for and you should get a popup with for snippet selected -> press Tab to insert body and autoselect index variable type -> type new type -> press Tab to select index variable name and so on; finally press Enter to jump into loop body.
It's a bug that was introduced in VS2017. Make sure to bump the thread dedicated to this issue, and hopefully this will encourage the VS team to prioritize fixing this bug in a future release.

Vim: autocomplete local variables/functions with clang_complete

I am new to vim and I am trying to set it up for use with C/C++. After reading about possible plugins for autocompletion I decided to try clang_complete.
I installed it and made sure it is working by using the included example file.
Completion after typing ::, -> or . works, but I just can't figure out how to get a autocompletion menu for local variables and functions defined in the same file or included via header.
Example:
void foobar();
void main()
{
foobar();
}
When typing foo in main(), would expect clang_complete to be able to complete it to foobar.
Did I miss anything when reading the clang_complete documentation or is this really not possible?
The answer to the question "how to open the autocompletion menu" is to click < C-x > < C-u > (which means firstly press ctrl and x at the same time, then ctrl and u). Then you'll se a menu of user defined auto completions which is where clang_complete's completions are. If you're as lazy as me and you want the completions to popUp automatically, install an additional plugin like AutoComplPop. You can also switch to YouCompleteMe, which I don't like but may suit you needs better.

How do I make Visual Studio pause after executing a console application in debug mode?

I have a collection of Boost unit tests I want to run as a console application.
When I'm working on the project and I run the tests I would like to be able to debug the tests, and I would like to have the console stay open after the tests run.
I see that if I run in release mode the console window stays up after the program exits, but in debug mode this is not the case.
I do not want to add 'system("pause");' or any other hacks like reading a character to my program. I just want to make Visual Studio pause after running the tests with debugging like it would if I were running in release mode. I would also like it if the output of tests were captured in one of Visual Studio's output windows, but that also seems to be harder than it should be.
How can I do this?
Try to run the application with the Ctrl + F5 combination.
http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5
In the older versions it would default to the console subsystem even if you selected "empty project", but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "project_name properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.
Boost test offers the following usage recommendations for Visual Studio that would enable you to run the unit tests automatically at the end of compilation and capture the output into the build window.
The nice side effect of this trick is it enable you to treat test failures as compilation errors. "...you could jump through these errors using usual keyboard shortcuts/mouse clicks you use for compilation error analysis..."
Set a breakpoint on the last line of code.
I just copied from http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787/?prof=required:
The following works for me :-)
/////////////////////////////////////////////////////////////////////////////////////
Here is another reason the console may disappear. And the solution:
With the new Visual Studio 2010 you might see this behavior even when you use Ctrl + F5 aka "start without debugging". This is most likely because you created an "empty project" instead of a "Win32 console application". If you create the project as a "Win32 console application" you can disregard this as it does not apply.
In the older versions it would default to the console subsystem even if you selected "empty project", but not in Visual Studio 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this).
Then select "project" from the menu bar drop down menus, then select "project_name properties" → "configuration properties" → "linker" → "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.
/////////////////////////////////////////////////////////////////////////////////////
If it is a console application, use Ctrl + F5.
In Boost.Test there is the --auto_start_dbg parameter for breaking into the debugger when a test fails (on an exception or on an assertion failure). For some reason it doesn't work for me.
See http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/usage-recommendations/dot-net-specific.html
For this reason I have created my custom test_observer that will break into the debugger when there is an assertion failure or an exception. This is enabled on debug builds when we are running under a debugger.
In one of the source files of my unit test EXE file I have added this code:
#ifdef _DEBUG
#include <boost/test/framework.hpp>
#include <boost/test/test_observer.hpp>
struct BoostUnitTestCrtBreakpointInDebug: boost::unit_test::test_observer
{
BoostUnitTestCrtBreakpointInDebug()
{
boost::unit_test::framework::register_observer(*this);
}
virtual ~BoostUnitTestCrtBreakpointInDebug()
{
boost::unit_test::framework::deregister_observer(*this);
}
virtual void assertion_result( bool passed /* passed */ )
{
if (!passed)
BreakIfInDebugger();
}
virtual void exception_caught( boost::execution_exception const& )
{
BreakIfInDebugger();
}
void BreakIfInDebugger()
{
if (IsDebuggerPresent())
{
/**
* Hello, I know you are here staring at the debugger :)
*
* If you got here then there is an exception in your unit
* test code. Walk the call stack to find the actual cause.
*/
_CrtDbgBreak();
}
}
};
BOOST_GLOBAL_FIXTURE(BoostUnitTestCrtBreakpointInDebug);
#endif
You say you don't want to use the system("pause") hack. Why not?
If it's because you don't want the program to prompt when it's not being debugged, there's a way around that. This works for me:
void pause () {
system ("pause");
}
int main (int argc, char ** argv) {
// If "launched", then don't let the console close at the end until
// the user has seen the report.
// (See the MSDN ConGUI sample code)
//
do {
HANDLE hConsoleOutput = ::GetStdHandle (STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE == hConsoleOutput)
break;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (0 == ::GetConsoleScreenBufferInfo (hConsoleOutput, &csbi))
break;
if (0 != csbi.dwCursorPosition.X)
break;
if (0 != csbi.dwCursorPosition.Y)
break;
if (csbi.dwSize.X <= 0)
break;
if (csbi.dwSize.Y <= 0)
break;
atexit (pause);
} while (0);
I just paste this code into each new console application I'm writing. If the program is being run from a command window, the cursor position won't be <0,0>, and it won't call atexit(). If it has been launched from you debugger (any debugger) the console cursor position will be <0,0> and the atexit() call will be executed.
I got the idea from a sample program that used to be in the MSDN library, but I think it's been deleted.
NOTE: The Microsoft Visual Studio implementation of the system() routine requires the COMSPEC environment variable to identify the command line interpreter. If this environment variable gets messed up -- for example, if you've got a problem in the Visual Studio project's debugging properties so that the environment variables aren't properly passed down when the program is launched -- then it will just fail silently.
It would actually be more effort, but you could just build in VS.Net, run it from the regular command line (cmd.exe), and then attach to the process after it starts running. This is probably not the solution you are looking for however.
Or you could use boost_test "Test Log Output."
http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-output/test-log.html
Then it won't matter whether the console window shows up at all AND your build logging can preserve the unit testing output as an artifact for examination on failed builds...
Adding the following line will do a simple MS-DOS pause displaying no message.
system("pause >nul | set /p \"=\"");
And there is no need to Ctrl+F5 (which will make your application run in Release Mode)
I would use a "wait"-command for a specific time (milliseconds) of your own choice. The application executes until the line you want to inspect and then continues after the time expired.
Include the <time.h> header:
clock_t wait;
wait = clock();
while (clock() <= (wait + 5000)) // Wait for 5 seconds and then continue
;
wait = 0;
You could also setup your executable as an external tool, and mark the tool for Use output window. That way the output of the tool will be visible within Visual Studio itself, not a separate window.
I start the app with F11 and get a breakpoint somewhere in unit_test_main.ipp (can be assembly code). I use shift-f11 (Step out) to run the unit test and get the next assembly instruction in the CRT (normally in mainCRTStartup()). I use F9 to set a breakpoint at that instruction.
On the next invocation, I can start the app with F5 and the app will break after running the tests, therefore giving me a chance to peek at the console window
Just use a logging library, like log4net, and have it log to a file appender.
Prompt for user input.
https://www.youtube.com/watch?v=NIGhjrWLWBo
shows how to do this for C++. For Node.js, this is taken right from the docs (and it works):
'use strict';
console.log('Hello world');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Press enter to continue...', (answer) => {
rl.close(); /* discard the answer */
});
Do a readline at the end (it's the "forma cochina", like we say in Colombia, but it works):
static void Main(string[] args)
{
.
.
.
String temp = Console.ReadLine();
}