C++20 Features in Visual Studio 2019 versions 16.7 and 16.8
We have continued our efforts to implement C++20 in the MSVC toolset, with noteworthy progress being made in VS (Visual Studio) 2019 v16.7 and VS 2019 v16.8. This blog post lists the features implemented since the C++20 Features and Fixes in VS 2019 16.1 through 16.6 blog post.
This blog post will specifically focus on language features implemented in the MSVC compiler (cl.exe). The changelog for conformance progress for the STL is tracked in the Standard Library changelog in the Microsoft STL GitHub repo.
Some of the features covered were already highlighted in the announcements made at CppCon 2020 in September related to the VS 2019 v16.8 Preview 3 release.
As a reminder, the /std:c++17 and /std:c++latest compiler options are necessary to use C++17 and C++20 features, respectively.
VS 2019 v16.7
C++20 Language Feature Support
- Implemented P1009R2 Array size deduction in new-expressions
- Implemented P0722R3 Efficient sized delete for variable sized classes
- Implemented P1094R2 Nested inline namespaces
- Implemented P1331R2 Permitting trivial default initialization in constexpr contexts
- Implemented P1816R0 CTAD for aggregates
- Implemented P0388R4 Permit conversions to arrays of unknown bound
- Implemented P1452R2 On the non-uniform semantics of return-type-requirements
- Implemented P1152R4 Deprecating volatile
- Implemented P1814R0 CTAD for alias templates
- Implemented P1972R0 US105 Check satisfaction of constraints for non-templates…
- Implemented P1980R0 CA096: Declaration matching for non-dependent requires-clauses
- Implemented P0528R3 Atomic compare-and-exchange with padding bits
- Implemented P1358R0 Type completeness and derived-to-base pointer conversions
- Implemented P0466R5 Layout-Compatibility and Pointer-Interconvertibility Traits
- Implemented P2107R0 US064 Copy semantics of coroutine parameters
- Implemented P2107R0 US052 Non-executed return statements in coroutines
- Implemented P2103R0 Allow “import” inside linkage-specifications [dcl.link]
- Implemented P2085R0 Consistent defaulted comparisons
- Implemented CWG 2280 Matching a usual deallocation function with placement new
- Implemented CWG 2382 Array allocation overhead for non-allocating placement new
- Implemented CWG 2441 Inline function parameters
- Implemented CWG 2282 Consistency with mismatched aligned/non-over-aligned allocation/deallocation functions
- Implemented US212 Converting from T* to bool should be considered narrowing
VS 2019 v16.8
C++20 Language Feature Support
- Implemented P0315R4 Allowing lambdas in unevaluated contexts
- Implemented P0960R3 Allow initializing aggregates from a parenthesized list of values
- Implemented P0848R3 Conditionally trivial special member functions
- Implemented P1811R0 Relaxing redefinition restrictions for re-exportation robustness
- Implemented P1766R1 Mitigating minor modules maladies
- Implemented P1975R0 Fixing the wording of parenthesized aggregate-initialization
- Implemented P1874R1 Dynamic Initialization Order of Non-Local Variables in Modules
Two of the major features of C++20, Modules and Coroutines, are now feature-complete in the MSVC toolset and are now implied by the /std:c++latest compiler switch.
Strict conformance mode (/permissive-) and /std:c++latest
As a prerequisite for Modules support, `/permissive-` is now enabled when /std:c++latest is specified. See /permissive- documentation regarding compiling code under `/permissive-`.
For code that previously compiled under /std:c++latest and requires non-conformant compiler behaviors, `/permissive` may be specified to turn off strict conformance mode in the compiler and must appear after /std:c++latest in the command line argument list.
However, specifying `/permissive` will result in an error if Modules usage is encountered:
error C1214: Modules conflict with non-standard behavior requested via ‘[option]’
Where the most common values for [option] are:
- `/Zc:twoPhase–` : Two Phase name lookup (`/Zc:twoPhase`) is required for C++20 Modules and implied by `/permissive-`. See also [/Zc:twoPhase documentation](https://docs.microsoft.com/en-us/cpp/build/reference/zc-twophase?view=vs-2019).
- `/Zc:hiddenFriend–` : Standard hidden friend name lookup rules are required for C++20 Modules and implied by `/permissive-`.
- `/Zc:preprocessor–` : Use of the conformant preprocessor (/Zc:preprocessor) is required for C++20 header unit usage and creation only. Named Modules do not require this option. See also /Zc:preprocessor documentation](https://docs.microsoft.com/en-us/cpp/build/reference/zc-preprocessor?view=vs-2019)
For Visual Studio changes (beyond the C++ toolset) and download links, see the VS Release Notes and VS Preview Release Notes. You can report bugs through Developer Community, and you can also report STL bugs via microsoft/STL GitHub issues.
Modules doesn’t work, i tried both the latest version of VS and the Preview
and import std; shows in red
I have enabled modules and latest c++ version on the project settings
What else should i do?
From the previous blog post
The std.* Modules which ship with Visual Studio will not be available through /std:c++latest alone. The standard library Modules have not yet been standardized and as such remain experimental. To continue using the standard library Modules users will need /experimental:module as part of their command line options.
Thank you for the reply Stanislav!
That is correct. ISO C++ has not yet standardized how the standard library will be organized with Modules. This is something that is being worked on for a future standard revision (hopefully C++23).
Tried to modularize my pet project – got a lot of “compiler internal error”(s). All errors occur around “global module fragment”
Can I ask you to file a bug on DevComm? We are working hard to improve the compiler as soon as we find issues.
import STL headers doesn’t work with /experimental:module and /std:c++latest
One thing to note about the header import syntax is that you need to write the header-name as if it was the argument to an `#include` directive:
The story around the STL headers being importable is being tracked by GH-60. Other header units for your project may be created and imported through the IDE experience documented here: https://devblogs.microsoft.com/cppblog/standard-c20-modules-support-with-msvc-in-visual-studio-2019-version-16-8/#project-system.
Please let me know if you have any additional questions.
Bogdan: Thank you for providing the link to docs on this topic!
The LibreOffice team would like to vote for std::source_location 🙂
We’re using it to add source information to exception throwing, which makes debugging much more pleasant
Thank you Noel!
Support for std::source_location is on the short list of remaining C++20 features and I know that work is being done targeting a future VS release. You can follow progress of C++20 STL features here: https://github.com/microsoft/STL/projects/3.
From the language/compiler perspective, I know that work is currently in progress that will enable the completion of this feature.
Hey friends, i wan’t to start developing desktop applications using visual studio, what do i need to learn for that, what steps to follow and what libraries to learn. Can u help me how to start using visual studio for that.