{"id":6273,"date":"2015-03-25T03:21:03","date_gmt":"2015-03-25T02:21:03","guid":{"rendered":"http:\/\/sysprogs.com\/w\/?p=6273"},"modified":"2016-07-16T22:24:47","modified_gmt":"2016-07-16T21:24:47","slug":"the-new-create-from-use-engine-makes-c-coding-easier","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/w\/the-new-create-from-use-engine-makes-c-coding-easier\/","title":{"rendered":"The new Create-from-use engine makes C++ coding easier"},"content":{"rendered":"<p>One of the annoyances\u00a0of C++ compared to higher-level languages like C# is the relatively large overhead\u00a0when creating new methods or functions. Unlike C# where you can just call the\u00a0not-yet-existing method and then select &#8220;create method stub&#8221;, C++ makes things harder: creating a new method\u00a0normally means manual\u00a0edits to both header and source files,\u00a0that can quickly get annoying.<\/p>\n<p>To\u00a0help VisualGDB users\u00a0improve 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.<br \/>\n<!--more--><\/p>\n<p>Let&#8217;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.\u00a0It starts just like in C# with calling a method that does not exist yet and selecting &#8216;create method stub&#8217; using a smart tag:<\/p>\n<p><a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newmethod.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6275\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newmethod.png\" alt=\"newmethod\" width=\"521\" height=\"199\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newmethod.png 521w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newmethod-300x115.png 300w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/a>Unlike C# where\u00a0a variable type\u00a0is simply a\u00a0class name following\u00a0its namespace, C++ makes things harder: the same &#8216;strings&#8217; variable could be\u00a0delcared in many different ways:<\/p>\n<ul>\n<li>std::vector&lt;std::string&gt; strings<\/li>\n<li>const std::vector&lt;std::string&gt; &amp;strings<\/li>\n<\/ul>\n<p>Or even\u00a0using the full type name:<\/p>\n<ul>\n<li>const std::vector&lt;std::basic_string&lt;char&gt;, std::allocator&lt;std::basic_string&lt;char&gt; &gt; &gt; &amp;strings<\/li>\n<\/ul>\n<p>So instead of guessing what would suit the best in each\u00a0specific case\u00a0we have decided to give control to our users and provided a powerful preview dialog:<\/p>\n<p><a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/methodwiz.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6276\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/methodwiz.png\" alt=\"methodwiz\" width=\"838\" height=\"626\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/methodwiz.png 838w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/methodwiz-300x224.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a>With a few hotkeys you can quickly\u00a0tune how the method\u00a0signature will\u00a0look like:<\/p>\n<ul>\n<li>Whether complex types are\u00a0passed by value or by reference<\/li>\n<li>Whether typedefs are resolved or written as declared<\/li>\n<li>Whether the method\u00a0body will be inline or will go to a source file<\/li>\n<\/ul>\n<p>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\u00a0from\u00a0another class, you can also\u00a0select to create the method in the base class.<\/p>\n<p>Now you can make final adjustments to\u00a0the method signature in the preview box (e.g. tab through argument names to rename them) and\u00a0click &#8220;OK&#8221; \u00a0and VisualGDB will generate the signature and implementation template for you so that you can begin writing the method body:<\/p>\n<p><a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/impl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6277\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/impl.png\" alt=\"impl\" width=\"517\" height=\"220\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/impl.png 517w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/impl-300x128.png 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a>By the way,\u00a0if you have bracket-level\u00a0alignment 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.<\/p>\n<h2>\u00a0Constructors<\/h2>\n<p>Another big annoying thing about C++ is the amount of work required to\u00a0add new fields to a structure and make a new constructor initializing them.\u00a0Let&#8217;s look at another example: we want to make a class encapsulating a log message (storing its component, indentation level and text):<\/p>\n<p><a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/noconstr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6279\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/noconstr.png\" alt=\"noconstr\" width=\"702\" height=\"87\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/noconstr.png 702w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/noconstr-300x37.png 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/a><\/p>\n<p>We would normally need\u00a0to write a lot of repeating code:<\/p>\n<ul>\n<li>Manually create 3 fields in the Message class<\/li>\n<li>Manually add a constructor with 3 arguments<\/li>\n<li>Manually write an initialization list assigning argument values to fields<\/li>\n<\/ul>\n<p>The new\u00a0IntelliSense engine\u00a0can fully automate this job for you. Start with selecting &#8220;Create constructor and fields&#8221;:<a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldstag.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6281\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldstag.png\" alt=\"fieldstag\" width=\"704\" height=\"136\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldstag.png 704w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldstag-300x58.png 300w\" sizes=\"(max-width: 704px) 100vw, 704px\" \/><\/a><\/p>\n<p>Now you can select which of the arguments will be converted to fields and how the fields will be named:<a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/initwiz.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6282\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/initwiz.png\" alt=\"initwiz\" width=\"576\" height=\"256\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/initwiz.png 576w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/initwiz-300x133.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a>If you just click OK, VisualGDB will generate an constructor stub with an initialization list for you.\u00a0So make minor adjustments like removing &#8216;&amp;&#8217; in\u00a0the field declarations in the preview field and you&#8217;re done:<a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldpreview.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6283\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldpreview.png\" alt=\"fieldpreview\" width=\"838\" height=\"626\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldpreview.png 838w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/fieldpreview-300x224.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a>The generated constructor can be placed in the\u00a0header file or moved to the source file just like a normal method:<\/p>\n<p><a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6284\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctor.png\" alt=\"ctor\" width=\"444\" height=\"164\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctor.png 444w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctor-300x111.png 300w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/a><\/p>\n<h2>Inherited classes<\/h2>\n<p>We also made it easy to generate constructors for\u00a0classes 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&#8217; constructor. VisualGDB will suggest that option for matching field\/argument names by default:<a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctorwiz2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6285\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctorwiz2.png\" alt=\"ctorwiz2\" width=\"576\" height=\"389\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctorwiz2.png 576w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/ctorwiz2-300x203.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a>As a result, you won&#8217;t have to remove unnecessary fields from your\u00a0class\u00a0or adjust the initialization list manually:<a href=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newfld.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6286\" src=\"http:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newfld.png\" alt=\"newfld\" width=\"838\" height=\"626\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newfld.png 838w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2015\/03\/newfld-300x224.png 300w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a>You can try out the new features by downloading the latest preview of VisualGDB 5.0 with the new IntelliSense engine here: <a href=\"http:\/\/visualgdb.com\/download\/\">http:\/\/visualgdb.com\/download\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the annoyances\u00a0of C++ compared to higher-level languages like C# is the relatively large overhead\u00a0when creating new methods or functions. Unlike C# where you can just call the\u00a0not-yet-existing method and then select &#8220;create method stub&#8221;, C++ makes things harder: creating a new method\u00a0normally means manual\u00a0edits to both header and source files,\u00a0that can quickly get &hellip; <a href=\"https:\/\/sysprogs.com\/w\/the-new-create-from-use-engine-makes-c-coding-easier\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The new Create-from-use engine makes C++ coding easier<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/6273"}],"collection":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/comments?post=6273"}],"version-history":[{"count":7,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/6273\/revisions"}],"predecessor-version":[{"id":8573,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/6273\/revisions\/8573"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/media?parent=6273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/categories?post=6273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/tags?post=6273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}