One of the annoyances of C++ compared to higher-level languages like C# is the relatively large overhead when creating new methods or functions. Unlike C# where you can just call the not-yet-existing method and then select “create method stub”, C++ makes things harder: creating a new method normally means manual edits to both header and source files, that can quickly get annoying.
To help VisualGDB users improve their productivity the newest VisualGDB 5.0 Preview 2 build contains a special create-from-use engine that automates method creation in a smart way.
Let’s look at a simple example: we are creating a class that logs various messages to a file and we want to add a new method accepting a vector of strings. It starts just like in C# with calling a method that does not exist yet and selecting ‘create method stub’ using a smart tag:
- std::vector<std::string> strings
- const std::vector<std::string> &strings
Or even using the full type name:
- const std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > > &strings
So instead of guessing what would suit the best in each specific case we have decided to give control to our users and provided a powerful preview dialog:
- Whether complex types are passed by value or by reference
- Whether typedefs are resolved or written as declared
- Whether the method body will be inline or will go to a source file
You can also pick a specific point in the class declaration where you want the method to be placed. And if your class is derived from another class, you can also select to create the method in the base class.
Now you can make final adjustments to the method signature in the preview box (e.g. tab through argument names to rename them) and click “OK” and VisualGDB will generate the signature and implementation template for you so that you can begin writing the method body:
By the way, if you have bracket-level alignment enabled in your VS code formatting settings, VisualGDB will honor that when generating method signatures and implementations, so long argument lists will always look neat and readable.
Another big annoying thing about C++ is the amount of work required to add new fields to a structure and make a new constructor initializing them. Let’s look at another example: we want to make a class encapsulating a log message (storing its component, indentation level and text):
We would normally need to write a lot of repeating code:
- Manually create 3 fields in the Message class
- Manually add a constructor with 3 arguments
- Manually write an initialization list assigning argument values to fields
Now you can select which of the arguments will be converted to fields and how the fields will be named:If you just click OK, VisualGDB will generate an constructor stub with an initialization list for you. So make minor adjustments like removing ‘&’ in the field declarations in the preview field and you’re done:The generated constructor can be placed in the header file or moved to the source file just like a normal method:
We also made it easy to generate constructors for classes that are derived from other classes and have existing fields. Instead of creating a new field for the argument you can simply route it to an existing field or to a base class’ constructor. VisualGDB will suggest that option for matching field/argument names by default:As a result, you won’t have to remove unnecessary fields from your class or adjust the initialization list manually:You can try out the new features by downloading the latest preview of VisualGDB 5.0 with the new IntelliSense engine here: http://visualgdb.com/download/