From the blog

Create C++ Member Function in Visual Studio

We are excited to announce that Create C++ Member Function can now be used to quickly add constructor and in Visual Studio 17.6 Preview 2. When you have a class with fields, you can add a default constructor, constructor with all fields, equality operator, and . Three dots will appear below a class name to indicate that you can add a member function, and you can hover over them to see the quick action (screwdriver icon). When the default constructor and equality operator are added respectively, the Go to Definition of the operator== is displayed below, showing that the body of the   You can also choose to add a constructor with all fields and an equality operator with all fields respectively, and the Go to Definition will show that the operator== has all the field comparisons.     Future Work This experimental feature will be improved by adding more functions that can save you a lot of typing. Right now, it includes constructor and operator==and we are considering adding more cases,   We have a Developer Community ticket to Improve “Create Member Function”, if you have the same suggestions make sure to upvote it.   Send us your feedback!  Download the latest version of Visual Studio Preview and give the Create C++ Member Function feature a try. Your feedback will be extremely helpful in shaping this experience, therefore, please continue to send your feedback in the comments below or/and via Developer Community. You can also reach us on Twitter (@VisualC), or via email at visualcpp@microsoft.com.   

Read More

Maximize Unreal Engine Development with the Latest Integrations in Visual Studio 2022

Since our announcement last month, the team has been working hard on building a new round of Unreal Engine integrations. Today, we are happy to show you the next set of features that can level up your game development productivity. In this article, you will learn about how to stream Unreal Logs, see Unreal Header Tool warnings in Visual Studio, and discover how you can be more productive working with HLSL files. All features mentioned below are available in the latest Visual Studio 2022 Preview. Curious to see these features in action? I chatted with Leslie Richardson in a special edition of Visual Studio Toolbox to demo many of the recently available game dev features in Visual Studio. Setting Up Unreal Engine Integrations Unreal Engine integrations will only show up when you are working on an Unreal Engine project. To ensure these features are active, double check that “IDE support for Unreal Engine” component is enabled in the “Game development for C++” workload in the VS Installer. Stream Unreal Engine Log We spoke with Unreal Engine developers and discovered the frustration of having to switch between tasks. Furthermore, this was particularly painful when they need to frequently switch between the UE editor and Visual Studio. As part of the continued efforts to reduce this pain, we are happy to introduce the ability to see your Unreal Engine logs without leaving Visual Studio. Upon pressing F5, Visual Studio will stream Unreal Engine logs to the UE Log window. To see the logs from the Unreal Engine Editor, click View -> Other Windows -> UE Log. When the Visual Studio Debugger is attached to your game, logs are streamed automatically. Alternatively, you can enable cross-process capturing by pressing the “Record” button. As a result, you can stream logs even when the debugger is not attached. To filter your logs, click on the “Categories” or “Verbosity” dropdowns This feature is currently an experimental feature, we would greatly appreciate feedback from you. Leave your thoughts by commenting on Unreal Engine Log Feedback. Code Analysis for Unreal Engine Code Analysis is an important part of the software development workflow. By creating opportunities to identify potential errors prior to compilation, you can save valuable time in your developer inner-loop. Today, we are adding the first of many Unreal Engine specific Code Analysis checks designed to make you more productive. In Visual Studio, you can now see warnings and errors generated by the Unreal Header Tool. Upon saving a file, Visual Studio will run the Unreal Header Tool in the background. Next, Visual Studio will display any warnings or errors in the Error List or as a purple squiggle in the editor. For additional information, please visit the documentation page for Unreal Header Tool. This feature is off-by-default. To enable the feature, please go to “Tools -> Options -> Environment -> Preview Features” and check the box by “Code Analysis with Unreal Header Tool (C++)”. We look forward to hearing from you about UE Code Analysis. Please leave feedback by commenting in the UE Code Analysis Feedback Ticket. Stay tuned for more Code Analysis checks in the upcoming previews. Creating shaders in game development is an important workflow for game developers. We worked with Tim Jones, the author of the popular HLSL Tools extension, to bring syntax […]

Read More

C++23’s New Fold Algorithms

C++20 added new versions of the standard library algorithms which take ranges as their first argument rather than iterator pairs, alongside other improvements. However, key algorithms like std::accumulate were not updated. This has been done in C++23, with the new std::ranges::fold_* family of algorithms. The standards paper for this is P2322 and was written by Barry Revzin. It been implemented in Visual Studio 2022 version 17.5. In this post I’ll explain the benefits of the new “rangified” algorithms, talk you through the new C++23 additions, and explore some of the design space for fold algorithms in C++. Background: Rangified Algorithms C++20’s algorithms make several improvements to the old iterator-based ones. The most obvious is that they now can take a range instead of requiring you to pass iterator pairs. But they also allow passing a “projection function” to be called on elements of the range before being processed, and the use of C++20 concepts for constraining their interfaces more strictly defines what valid uses of these algorithms are. These changes allow you to make refactors like: // C++17 algorithm cat find_kitten(const std::vector& cats) { return *std::find_if(cats.begin(), cats.end(), [](cat const& c) { return c.age == 0; }); } // C++20 algorithm cat find_kitten(std::span cats) { return *std::ranges::find(cats, 0, &cat::age); } The differences here are: Instead of having to pass cats.begin() and cats.end(), we just pass cats itself. Since we are comparing a member variable of the cat to 0, in C++17 we need to use std::find_if and pass a closure which accesses that member and does the comparison. Since the rangified algorithms support projections, in C++20 we can use std::ranges::find and pass &cat::age as a projection, getting rid of the need for the lambda completely. These improvements can greatly clean up code which makes heavy use of the standard library algorithms. Unfortunately, alongside the algorithms which reside in the header, there are also several important ones in the header, and these were not rangified in C++201. In this post we’re particularly interested in std::accumulate and std::reduce. accumulate and reduce std::accumulate and std::reduce are both fold operations. They “fold” or “reduce” or “combine” multiple values into a single value. Both take two iterators, an initial value, and a binary operator (which defaults to +). They then run the given operator over the range of values given by the iterators, collecting a result as they go. For instance, given std::array arr = {1,2,3}, std::accumulate(begin(arr), end(arr), 0, std::plus()) will run (((0 + 1) + 2) + 3). Or std::accumulate(begin(arr), end(arr), 0, f) will run f(f(f(0, 1), 2), 3). These functions are both what are called left folds because they run from left to right. There’s also right folds, which as you may guess, run from right to left. For the last example a right fold would look like f(1, f(2, f(3, 0))). For some operations, like +, these would give the same result, but for operations which are not associative (like -), it could make a difference. So why do we have both std::accumulate and std::reduce? std::reduce was added in C++17 as one of the many parallel algorithms which let you take advantage of parallel execution for improved performance. The reason it has a different name than std::accumulate is because it has different constraints on what types and operations you can use: namely the […]

Read More

Even faster builds with Incredibuild 10 and Visual Studio 17.6 Preview 3

Note: This post was co-authored with Incredibuild’s Director of Product Marketing, Yohai West. We are pleased to announce that Visual Studio version 17.6 Preview 3 includes Incredibuild’s most advanced developer acceleration platform: Incredibuild 10. This release includes several notable, new features that empower teams to speed up the development process: Patent-pending Build Cache technology allows developers to cache build outputs so that they can be reused by all team members. A smart and flexible enterprise license management mechanism, managed via a new web-based Coordinator user interface. Incredibuild Cloud optimization automatically manages the best mix of on-demand and spot resources, enabling organizations to use smaller and more affordable machines, while maintaining optimal performance and cost. In this post we’ll detail how these features can improve your daily development. Build Cache – cache what you can; distribute the rest Incredibuild 10’s most significant addition is its Build Cache technology. Incredibuild breaks down development processes into smaller tasks that can be executed independently, and Build Cache saves time and resources by reusing the cached outputs for previously executed tasks. Build Cache extends the incremental builds you are already likely familiar with by providing access to the outputs that your entire team has created. This means that an incremental build only has to build your changes and can rely on the cache when you have merged a teammate’s unrelated changes into yours. Additionally, temporarily working on a different branch won’t cause a large build when you switch back to your original branch. For any task outputs that are not in the cache, the Incredibuild Grid can distribute those tasks across its pool of compute cores. The Incredibuild Grid can allocate a pool of machines to meet the needed capacity for the tasks, including on-premises machines and static and on-demand spot instances provided by Incredibuild Cloud. The machines in the grid don’t need a compiler to be installed or the code to be present, as the Incredibuild Grid takes care of it all. Once those tasks are completed and cached using Build Cache, they never have to be executed again, dramatically reducing build times for your entire team. These features working in tandem means that you can cache what you can; distribute the rest. Build from home without impacting your speed Working from home affects build speeds due to limited upstream bandwidth. Build Cache lets you rely more on downstream bandwidth, giving you greater speed and better performance when starting new builds. With Build Cache you can reuse previous build data stored on your local machine to dramatically reduce build times without impacting your bandwidth. Deployment Examples Build Cache can be deployed in different ways depending on how your Clients connect to Endpoints. A single Client can function as its own Endpoint (local), multiple Clients can connect to a single Endpoint (shared), and more complex or dynamic deployments are possible as well. Local Uses the same Initiator Agents to host the Build Cache Endpoint and Build Cache Client. This means that each agent can only benefit from the cache of builds that were previously run on the same machine. This can be ideal if you are not sharing code with other developers, or if you are working from home with limited bandwidth. Shared Uses a single Agent to host the Build Cache Endpoint. Multiple Initiator […]

Read More

Functional exception-less error handling with C++23’s optional and expected

This post is an updated version of one I made over five years ago, now that everything I talked about is in the standard and implemented in Visual Studio. In software things can go wrong. Sometimes we might expect them to go wrong. Sometimes it’s a surprise. In most cases we want to build in some way of handling these misfortunes. Let’s call them disappointments. std::optional was added in C++17 to provide a new standard way of expressing disappointments and more, and it has been extended in C++23 with a new interface inspired by functional programming. std::optional expresses “either a T or nothing”. C++23 comes with a new type, std::expected which expresses “either the expected T, or some E telling you what went wrong”. This type also comes with that special new functional interface. As of Visual Studio 2022 version 17.6 Preview 3, all of these features are available in our standard library. Armed with an STL implementation you can try yourself, I’m going to exhibit how to use std::optional‘s new interface, and the new std::expected to handle disappointments. One way to express and handle disappointments is exceptions: void pet_cat() { try { auto cat = find_cat(); scratch_behind_ears(cat); } catch (const no_cat_found& err) { //oh no be_sad(); } } There are a myriad of discussions, resources, rants, tirades, and debates about the value of exceptions123456, and I will not repeat them here. Suffice to say that there are cases in which exceptions are not the best tool for the job. For the sake of being uncontroversial, I’ll take the example of disappointments which are expected within reasonable use of an API. The Internet loves cats. Suppose that you and I are involved in the business of producing the cutest images of cats the world has ever seen. We have produced a high-quality C++ library geared towards this sole aim, and we want it to be at the bleeding edge of modern C++. A common operation in feline cutification programs is to locate cats in a given image. How should we express this in our API? One option is exceptions: // Throws no_cat_found if a cat is not found. image_view find_cat (image_view img); This function takes a view of an image and returns a smaller view which contains the first cat it finds. If it does not find a cat, then it throws an exception. If we’re going to be giving this function a million images, half of which do not contain cats, then that’s a lot of exceptions being thrown. In fact, we’re pretty much using exceptions for control flow at that point, which is A Bad Thing™. What we really want to express is a function which either returns a cat if it finds one, or it returns nothing. Enter std::optional. std::optional find_cat (image_view img); std::optional was introduced in C++17 for representing a value which may or may not be present. It is intended to be a vocabulary type — i.e. the canonical choice for expressing some concept in your code. The difference between this signature and the previous one is powerful; we’ve moved the description of what happens on an error from the documentation into the type system. Now it’s impossible for the user to forget to read the docs, because the compiler is reading them for us, […]

Read More

vcpkg 2023.04.15 Release: vcpkg ships in Visual Studio, Xbox triplets, GitHub Actions Cache Support, and More…

The 2023.04.15 release of the vcpkg package manager is available. This blog post summarizes changes from February 25th, 2023 to April 15th, 2023 for the Microsoft/vcpkg, Microsoft/vcpkg-tool, and Microsoft/vcpkg-docs GitHub repos. Some stats for this period: 39 new ports were added to the open-source registry. If you are unfamiliar with the term ‘port’, they are packages that are built from source and are typically C/C++ libraries. 957 updates were made to existing ports. As always, we validate each change to a port by building all other ports that depend on or are depended by the library that is being updated for our nine main triplets. There are now 2,190 total libraries available in the vcpkg public registry. 125 contributors submitted PRs, issues, or participated in discussions in the repo. The main vcpkg repo has over 5,300 forks and 18,400 stars on GitHub.   Notable Changes vcpkg now included with Visual Studio IDE As of Visual Studio 2022 (version 17.6), vcpkg is now added by default for IDE installations that include C++ workloads. Visual Studio users can run vcpkg commands from a Developer Command Prompt for Visual Studio targeting a new version of the IDE – both the ones embedded in the IDE as well as external terminals. You must run vcpkg integrate install to configure the built-in copy of vcpkg as the one for use by the IDE and in order to add enable MSBuild and CMake integration. More details will be shared in a separate blog post (coming soon!).   vcpkg now supports targeting Xbox out of the box with four new community triplets. The appropriate triplet is automatically chosen when using the Gaming.Xbox.*.x64 custom platforms in the Microsoft GDK with Xbox Extensions via the vcpkg integrate functionality. This makes it easy for developers to build open-source dependencies for this platform. Thank you to Chuck Walbourn from the Xbox organization for preparing these triplets. Triplet Description x64-xbox-scarlett Builds DLLs for Xbox Series X|S x64-xbox-scarlett-static Builds static C++ libraries for Xbox Series X|S x64-xbox-xboxone Builds DLLs for Xbox One x64-xbox-xboxone-static Builds static C++ libraries for Xbox One   Another benefit of these new triplets is for open-source library maintainers, who can validate that their code will build against the WINAPI_FAMILY_GAMES partition used on Xbox using only the public Windows SDK. To learn more about the new triplets and how to use them, visit Chuck’s blog post on the topic.   Official vcpkg Tool Release Architecture is Now amd64 vcpkg tool releases on GitHub are now amd64 (was previously x86), as we do not expect many users to prefer x86 going forward. PR: Microsoft/vcpkg-tool#1005   Binary Caching Support for GitHub Actions Cache The vcpkg binary caching feature now works with the GitHub Actions Cache. GitHub Actions Cache allows you to use a REST API to query and manage the cache for repositories in GitHub Actions. PR: Microsoft/vcpkg-tool#836 (thanks @quyykk!)   vcpkg use now stacks with previous use or activate commands (but not vice versa) For artifacts users, we have made the behavior of vcpkg use and vcpkg activate consistent when used multiple times. You can run vcpkg use after previously running vcpkg use on another artifact to enable both artifacts at the same time. You can also run vcpkg use after a previous vcpkg activate to enable all artifacts invoked by […]

Read More

Fill in the ISO C++ Developer Survey

The ISO C++ developer survey runs every year and is a great help to us in prioritizing work based on what the community cares about. It only takes about 10 minutes to complete and closes tomorrow, so please take the time to fill it out.   Sy Brand C++ Developer Advocate, C++ Team

Read More

Documentation for C++20 Ranges

C++20 introduced Ranges to the standard library: a new way of expressing composable transformations on collections of data. This feature adds a huge amount of expressive power and flexibility to C++. As with many concepts in C++, that power comes with new concepts to learn, and some complexity which can be difficult to navigate. One way of taming that complexity is through complete, clear, comprehensive documentation. Christopher Di Bella and Sy Brand (one of the co-authors of this post) presented their ideas for C++ documentation in the era of concepts in their CppCon 2021 talk. Tyler Whitney (the other co-author, and manager of our C++ documentation at Microsoft) has expanded these ideas and exhaustively documented the range adaptors available in the standard library for you. To check it out, go to on Microsoft Learn. In this post, we’ll talk through some of the complexity of using and documenting Ranges, and outline the principles behind the documentation which Tyler has written. But first, a quick introduction to Ranges for those of you who are not familiar with the feature. What are Ranges? At a high level, a range is something that you can iterate over. A range is represented by an iterator that marks the beginning of the range, and a sentinel that marks the end of the range. The sentinel may be the same type as the begin iterator, or it may be different, which lets Ranges support operations which simple iterator pairs can’t. The C++ Standard Library containers such as vector and list are ranges. A range abstracts iterators in a way that simplifies and amplifies your ability to use the Standard Template Library (STL). STL algorithms usually take iterators that point to the portion of the collection that they should operate on. For example, you could sort a vector with std::sort(myVector.begin(), myVector.end());. However, carrying out an algorithm across a range is such a common operation, we’d rather not have to retrieve the begin and end iterators every time. With ranges, you can instead call std::ranges::sort(myVector);. But perhaps the most important benefit of ranges is that you can compose STL algorithms that operate on ranges in a style that’s reminiscent of functional programming. Traditional C++ algorithms don’t compose well. For example, if you wanted to build a vector of squares from the elements in another vector that are divisible by three, you could write something like: std::vector input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector intermediate, output; std::copy_if(input.begin(), input.end(), std::back_inserter(intermediate), [](const int i) { return i%3 == 0; }); std::transform(intermediate.begin(), intermediate.end(), std::back_inserter(output), [](const int i) {return i*i; }); With ranges, you can produce a range with the same values without the intermediate vector: // requires /std:c++20 std::vector input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; auto output = input     | std::views::filter([](const int n) {return n % 3 == 0; })     | std::views::transform([](const int n) {return n * n; }); Besides being easier to read, this code avoids the memory allocation that’s required for the intermediate vector and its contents. It also allows you to compose two operations. In the preceding code, each element that’s divisible by three is combined with an operation to square that element. The pipe (|) symbol chains the operations together and is read […]

Read More

What’s New for CMake Tools 1.14 in VS Code – Test Explorer

The team has been working hard to provide new highly requested capabilities for CMake users. Now, in version 1.14, we have provided a new Test Explorer for using CTest with your CMake projects. This release also features open-source community contributions from users. Thanks for your contributions! Test Explorer One of our most highly-upvoted tickets in the CMake Tools Extension was the request for a Test Explorer for CTest. We are excited to announce that this is now available for users in the latest version of the CMake Tools extension in VS Code. Now, in your CMake projects, you can click the “Run CTest” icon along the bottom status bar or the “Testing” side panel icon to launch the test explorer. When the project is configured, the tests in your CMakeLists.txt will load in the Test Explorer. In the Test Explorer, you can view the detailed state of all your tests and the last results of these test runs. By clicking on the play button, you can run a specific test (or set of tests). Also, by clicking on the debug play button, you can debug these tests. Using the topmost icon, you can view the output of these tests. You can refresh these tests at any time using the refresh icon up top. We are continuing to listen to and work on all feedback we receive on the Test Explorer, so if you have any issues or suggestions, please report them in the Issues section of our GitHub repository. Future Work Next up, we are planning to experiment with the CMake tools user experience settings and implement CMake language services and a CMake Debugger. Is there anything else you’d like to see? Let us know! What do you think? Download the CMake Tools extension for Visual Studio Code and let us know what you think. We would love to see what you contribute to our repo and are active on reviews and collaboration. Comment below or reach us via email at visualcpp@microsoft.com or via Twitter at @VisualC.  

Read More

5 New Posts About C, C++, And Python

Hello C++ Developers. In today’s round-up of recent posts on LearnCPlusPlus.org, we have 5 new C and C++ posts with some very simple examples that can be used with a modern C++ Compiler that supports C++17. We also dabble a little with Python too, just for good measure! Table of Contents Why is Python useful to C++ developers? Do you want to learn more about to create beautiful Python GUI applications? What are the important features of C and C++ programming? What new C++ posts are there today? Learn with examples about C and C++ today Learn what’s new next coming C++ Builder Why is Python useful to C++ developers? Python is one of my latest programming languages that I had to learn. I tried to stay avoid it for a long time – it’s a long story, so hear me out! You may not know, but the first release of TensorFlow, the well-known machine learning library, used C++ as the language. The TensorFlow team moved to Python. Then a ‘Lego Mindstorms course for students’ forced me to learn micro Python 4-5 years ago. Even though in my daily work I specialize in AI areas still I stayed away from using Python too often and my primary programming language is mostly C++ usually with C++ Builder. Python is hugely popular and has some really useful libraries. It is one of the favorite languages of new-generation programmers, including my son ???? Python is simple, and easy to learn, and it’s particularly strong in the field of AI and machine learning (ML). If you are a C++ developer, you might want your users to be able to analyze data with AI modules or frameworks written in the Python language in your applications. Let’s imagine you want the users to carry out a few buttons clicks to do some heavy AI analysis. That way you get the best of both worlds by having the raw speed and power of C++ which can run Python modules and make use of their vast ecosystem of libraries and routines. Do you know, you can use Python language in C++ very easily? This week, we will show how you can integrate your C++ application with the Python language.  Do you want to learn more about to create beautiful Python GUI applications? If you have not visited before you should definitely visit the PythonGUI.org website which is a superb resource with lots of great information, full working examples and a whole host of tips, tricks, and free libraries to help you create gorgeous GUI application with Python. Here’s a sample post from PythonGUI.org about some recent updates about Python4Delphi and the Delphi-Python EcoSystem. Latest updates in Python4Delphi and PythonEnvironments in Delphi-Python EcoSystem What are the important features of C and C++ programming? In the programming world today, Python programming language is very popular – perhaps second only to C++. Python is easy to use, and it is more popular because of support from big companies like Google. One of the areas Python is particularly successful are with libraries and frameworks for AI technology and machine learning. It is a great object-oriented, interpreted, and interactive programming language. Python has very clear syntax. It has modules, classes, exceptions, very high-level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as […]

Read More