{"id":7532,"date":"2016-02-11T23:48:04","date_gmt":"2016-02-11T23:48:04","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=7532"},"modified":"2019-02-18T18:04:50","modified_gmt":"2019-02-18T18:04:50","slug":"compiler-improvements-in-vs-2015-update-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/compiler-improvements-in-vs-2015-update-2\/","title":{"rendered":"Compiler improvements in VS 2015 Update 2"},"content":{"rendered":"<p>The C++ compiler team is excited for you to try out the compiler in <a title=\"Visual Studio Update 2 CTP 1\" href=\"https:\/\/www.visualstudio.com\/en-us\/news\/vs2015-update2-vs.aspx\">Visual Studio 2015 Update 2 CTP 1<\/a>. Since Update 1 we\u2019ve made progress on being Standards-conformant for lot of C++11 and C++14 features. One particularly big milestone is that <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2016\/01\/22\/vs-2015-update-2s-stl-is-c17-so-far-feature-complete\/\">our standard library in Update 2 supports every C++ Standard Library feature that\u2019s been voted into C++11, C++14, and C++17-so-far<\/a>. Parser improvements were required to make that happen, in addition to a lot of great work from our libraries team.\nWe\u2019ve also fixed about 300 compiler bugs since Update 1, many of which address bad code generation in our compiler. We understand that breaking changes can be very impactful for developers and so we strive to make sure any changes that can affect currently working source code are minor and rare and try our best to document any areas where bug fixes might affect you along with potential fixes. For example, we changed how we parse expressions inside of decltypes to better support expression SFINAE. And we have a new warning for what was previously ill-formed code that should better help you diagnose memory issues with mismatched new\/delete. For the whole list of parser improvements that may require source code changes along with suggestions of how to move your code forward, please visit the dedicated MSDN page: <a href=\"https:\/\/msdn.microsoft.com\/en-US\/library\/mt656697(VS.140).aspx\">Breaking Changes in Visual C++ 2015 Update 2<\/a>.\nLet\u2019s dive into some of the feature improvements we\u2019ve made in the compiler for Update 2. Many of these changes are helping our compiler to conform to existing C++11 and C++14 Standards features. And some of our changes are forward-looking; that is, we implemented features expected to be in the C++17 Standard. The full list we\u2019ll go through in this blog post are <code>constexpr<\/code>, variable templates, order of initializer list, modules, and working with UTF8 \u2013 let\u2019s dive in!<\/p>\n<h2>constexpr<\/h2>\n<p>We\u2019re continuing to improve our <code>constexpr<\/code> support. This is a feature that allows you to express computations that run at compile time instead of at runtime. If you\u2019re not familiar with <code>constexpr<\/code>, you should check out Cody Miller\u2019s blog post about <code>constexpr<\/code> here: <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/02\/constexpr-in-vs2015-update-1\">https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/02\/constexpr-in-vs2015-update-1<\/a>. We did a lot of work in Update 1 to enable <code>constexpr<\/code> support for features in the STL but we\u2019re still making progress on full C++11 and C++14 <code>constexpr<\/code> support.<\/p>\n<h2>Variable templates<\/h2>\n<p>Variable templates are a C++14 feature that we implemented to help our standard libraries ship C++17 features. If you\u2019ve ever used <code>std::is_same&lt;T, U&gt;::value<\/code> you\u2019ll really appreciate <code>std::is_same_v<\/code>. It\u2019s the variable template alias of the <code>std::is_same<\/code> type trait.\nVariable templates allow you to remove <code>::value<\/code> from C++11 code using type traits, e.g., <code>is_signed&lt;T&gt;::value<\/code>. In advanced cases, you can remove a C++11 workaround for having to declare a static data member of a wrapper class template that only exists so you have something to parameterize.\nSo what are variable templates and how might you go about using them in your code? Variable templates are a fix to the standard that allows the declaration of a variable in a template declaration. You can define a parameterized constant, for example:\n<code>template&lt;typename T&gt; constexpr T pi = T(3.1415926535897932385);<\/code>\nand use it in a function that computes the area of a circle:\n<code>template&lt;typename T&gt; T area_of_circle_with_radius(T r) { return pi&lt;T&gt; * r * r; }<\/code>\nPreviously a template declaration was only allowed to be a function, class, or alias. Now, in the MSVC compiler it can be a variable as well.\nNB: We&#8217;re still doing the work to support variable templates in IntelliSense in the VS IDE. We made an exception and shipped the feature because we had other dependencies on\u00a0variable templates\u00a0(i.e., the STL work mentioned above.)<\/p>\n<h2>Order of initializer list<\/h2>\n<p>An initializer list allows you to initialize data simply in a braced comma-separated list. For example, it\u2019s easier to write this\n<code>std::complex&lt;double&gt; z{1,2}<\/code>\nthan to allocate the double and then initialize its members.\nThe Standard specifies that initializer lists must be evaluated in the order that they appear. For example, in\n<code>a{x(), y(), z()}<\/code>\n<code>x()<\/code> must be called before <code>y()<\/code> which must be called before <code>z()<\/code>. One place where this is really critical is in iterating over the elements of a tuple. We\u2019ve gotten feedback about this not working in MSVC before, for example, here: <a href=\"https:\/\/gist.github.com\/DrPizza\/bf8cbdf63e7a9702e90a\">https:\/\/gist.github.com\/DrPizza\/bf8cbdf63e7a9702e90a<\/a>.\nIn Update 2 we\u2019re now calling initializer lists in order, according to the Standard. In places where we fail to do so\u2014usually because of nontrivial dependencies between classes in the initializer list\u2014we\u2019ll emit a warning.<\/p>\n<h2>C++ Modules<\/h2>\n<p>Our team has been working hard on C++ Modules, a new C++ feature that has been approved by the C++ Evolution Working Group for a Technical Specification, and currently under review by the C++ Core Working Group. Modules help you improve the componentization of your code, which in turn leads to build throughput improvements. Modules also isolate your code from preprocessor state which can be hard to debug. And they\u2019ll make it easier to develop tools to work with your code as you don\u2019t have to write a C++ preprocessor and parser to understand your interfaces.\nWe wrote an in-depth blog post about C++ Modules here: <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/03\/c-modules-in-vs-2015-update-1\">https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/03\/c-modules-in-vs-2015-update-1<\/a>.\nIf you tried out C++ Modules in Update 1, you might have noticed that a lot of code didn\u2019t work. We\u2019ve been working hard on getting this new feature implemented. Try out C++ Modules in Update 2 and you\u2019ll find a lot more code just works.<\/p>\n<h2>Working with UTF-8<\/h2>\n<p>We\u2019ve made it easier to work with UTF-8 and other character sets with MSVC. It\u2019s very hard to compile code that contains non-ASCII strings cross-platform\u2014we refer to this as the source character set. And because the compiler interprets strings differently based on what environment the program is run in, it\u2019s difficult to write code that will be compiled on different operating systems that treat non-ASCII strings the same everywhere. We refer to this as the execution character set.\nWe\u2019ve added several new command-line compiler options that allow you to specify explicitly the source and execution character sets. Because UTF-8 is the a commonly encountered character set with cross-platform code, we\u2019ve also introduced an option that sets both the source and execution character sets to UTF-8.\nThere\u2019s a great blog post by Jim Springfield that explains how we\u2019re handling character sets here: https:\/\/blogs.msdn.microsoft.com\/vcblog\/2016\/02\/22\/new-options-for-managing-character-sets-in-the-microsoft-cc-compiler\/<\/p>\n<h2>Empty base class optimization<\/h2>\n<p><em>NB: Empty base class optimization is in Update 2, not Update 2 CTP<\/em>\nSometimes you\u2019ll write an empty class to be the base of a hierarchy. But that empty class isn\u2019t really empty: C++ requires that a class have a non-zero size to ensure object identity. If, for a given class <code>C<\/code>, <code>sizeof(C)==0<\/code>, math done on a pointer to <code>C<\/code> could cause a divide by zero. (For example, how would you compute the number of elements between <code>x<\/code> and <code>y<\/code> in this expression: <code>&amp;a[x] - &amp;a[y]<\/code>? You\u2019d divide the distance between the pointers by the size of the elements which cannot be zero.) Because of this, empty classes are given a minimal size.\nBut while the size of any empty class is always non-zero, when an empty class is used as a base class it can have effectively zero size. There\u2019s a well-known optimization called \u201cempty base class optimization\u201d that makes an empty base class have zero size. MSVC previously only did this optimization in a limited fashion. With Update 2 we now support it broadly.\nCurrently you have to mark every class where you want this optimization performed with a <code>__declspec<\/code>. For example,<\/p>\n<pre><code>struct empty1 {};\nstruct empty2 {};\nstruct empty3 {};\nstruct __declspec(emptyBases) MyStruct : empty1, empty2, empty3\n{\n    int i;\n    int j;\n    int k;\n}\n<\/code><\/pre>\n<p>We also created a compiler option, <code>\/d1reportEmptyBasesClassLayout<\/code> to help identify classes where this optimization would be useful. The switch is what we call a \u201cd1\u201d switch, meaning that it\u2019s a temporary and unsupported compiler switch that will be removed one day without notice.\nWhy didn\u2019t we just turn on empty base class optimization by default? The optimization changes class layout, meaning that it would be a binary breaking change. We try to minimize breaking changes and we know that binary breaking changes are especially painful for developers. One day we\u2019ll be able to make this breaking change and you\u2019ll no longer need to use the <code>__declspec<\/code>. But for now, removing the size of empty base classes can save significant binary size in some codebases so we wanted you to be able to do the optimization where appropriate in your code.\nYou can read more about empty base class optimization in this blog post: <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2016\/03\/30\/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3\/\">https:\/\/blogs.msdn.microsoft.com\/vcblog\/2016\/03\/30\/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3\/ <\/a><\/p>\n<h2>In closing<\/h2>\n<p><a href=\"https:\/\/www.visualstudio.com\/en-us\/news\/vs2015-update2-vs.aspx\">You can enjoy even more new capabilities beyond the compiler in VS2015 Update 2<\/a>. We\u2019re eager for you to try out the changes and give us feedback in the comments below or through the usual channels: email, <a href=\"http:\/\/visualstudio.uservoice.com\/forums\/121579-visual-studio-2015\">Suggestions<\/a>, <a href=\"https:\/\/connect.microsoft.com\/visualstudio\">Connect<\/a> (for bugs), or even <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=517102\">Send-A-Smile<\/a>.\n<em>NB:\u00a0Edited this post on\u00a031 Mar\u00a0&#8217;16 to reflect that\u00a0Empty Base Class Optimization shipped in the full release of Update 2.<\/em>\nAlso note this post on Partial Support for Expression SFINAE in VS 2015 Update 1: <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/02\/partial-support-for-expression-sfinae-in-vs-2015-update-1\/\">https:\/\/blogs.msdn.microsoft.com\/vcblog\/2015\/12\/02\/partial-support-for-expression-sfinae-in-vs-2015-update-1\/<\/a>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ compiler team is excited for you to try out the compiler in Visual Studio 2015 Update 2 CTP 1. Since Update 1 we\u2019ve made progress on being Standards-conformant for lot of C++11 and C++14 features. One particularly big milestone is that our standard library in Update 2 supports every C++ Standard Library feature [&hellip;]<\/p>\n","protected":false},"author":312,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[140,100,65,151,36],"class_list":["post-7532","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-c","tag-c-language","tag-compiler","tag-iso-c","tag-vc"],"acf":[],"blog_post_summary":"<p>The C++ compiler team is excited for you to try out the compiler in Visual Studio 2015 Update 2 CTP 1. Since Update 1 we\u2019ve made progress on being Standards-conformant for lot of C++11 and C++14 features. One particularly big milestone is that our standard library in Update 2 supports every C++ Standard Library feature [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/7532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/312"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=7532"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/7532\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=7532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=7532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=7532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}