From the blog

Learn Strong Compare and Exchange In Modern C++

Since the C++11 standard, the Concurrency Support Library includes built-in support for threads (std::thread) with atomic operations (std::atomic). C++11 provides both weak and strong compare-and-exchange operations in multi-threading applications. Since C++11, strong compare and exchange are used in modern C++ standards such as C++14, C++17, C++20, and in other new standards. In this post, we explain what is strong compare and exchange is with simple examples. What is strong compare and exchange in C++? The Strong Compare and Exchange template atomically compares the value pointed to by the atomic object with the value pointed to by expected. This feature comes with C++11 and is used in other modern C++ standards, and performs the following operations based on the comparison results: If the comparison result is true (bitwise-equal), the function replaces the value pointed to by the atomic object with the value desired. If the comparison result is false, the function updates the value in expected with the value pointed by the atomic object. There are two most common syntaxes, here is the syntax for the compare_exchange_strong, first,   bool compare_exchange_strong( T& expected, T desired) noexcept;   and in strong compare and exchange atomic operations, we can use std::compare_exchange_strong template with memory order types. Here is the syntax for the compare_exchange_strong with memory orders,   bool compare_exchange_strong( T& expected, T desired,                               std::memory_order success,                               std::memory_order failure ) noexcept;   here is how we can use it,   std::atomic x;   exchanged = x.compare_exchange_strong( expected, desired);   exchanged = x.compare_exchange_strong( expected, desired, std::memory_order_release, std::memory_order_relaxed);   Note that providing both compare_exchange_strong and compare_exchange_weak allows us to decide whether we want the library to handle spurious failures (using compare_exchange_strong) or if we want to handle it in ourr own code (using compare_exchange_weak). The compare_exchange_strong needs extra overhead to retry in the case of failure. For details, please see Load-link/store-conditional in Wikipedia. Is there a simple example of strong compare and exchange in C++? Here is a simple example about strong compare and exchange in modern C++. Let’s assume we have a thread function myf1() that ensures value is changed after a strong compare and exchange. This is how we can do this,   void myf1(int desired) { int expected = 1; bool exchanged;   do { exchanged = x.compare_exchange_strong( expected, desired ); std::cout

Read More

Introducing CMake Debugger in VS Code: Debug your CMake Scripts using Open-Source CMake Debugger

Introducing CMake Debugger in VS Code: Debug your CMake Scripts using Open-Source CMake Debugger Sinem Akinci August 9th, 20231 2 The new CMake Debugger that was introduced in Visual Studio is now available in VS Code. Now, you can debug your CMakeLists.txt scripts from VS Code using the CMake Tools Extension. To see the full release notes for this release and what else is included, including bug fixes, please see the release notes. Background The Visual C++ team collaborated closely with Kitware, the CMake maintainers, to merge our CMake debugger implementation upstream and make this widely available. This implementation is now available in CMake version 3.27. Please download the latest version for your OS via this link or update via your system package manager to access the CMake debugger in VS Code. You can check your CMake version on your machine at any time by running cmake –version in a terminal window. CMake 3.27 will ship with Visual Studio in a later release in 17.8. The debugger uses the widely supported Debug Adapter Protocol, which is compatible with many development environments.  We are excited to see how the open-source community works together to implement new ideas for the debugger. CMake Debugger Functionality As a user, you’ll see the same functionality as you would in a normal debugging session. This includes viewing variables, call stacks, and cache variables specific to CMake and the ability to set breakpoints on your CMakeLists.txt file and step through your code. To open the CMake Debugger in your project, you can select it from the command palette by pressingCtrl+Shift+P. Additionally, it can be opened anywhere else you typically configure your project, such as in the CMake Project Outline in the CMake Tools side panel. If your CMake configure ever fails, a notification will pop-up for you to interact with to launch the debugger. What’s next? Next up, we are working on a few different things including implementing CMake language services and re-vamping our overall CMake side panel and status bar experiences based on user feedback. Let us know what else you’d like to see! Send us your feedback! We hope this helps your CMake workflows in VS Code. Download the latest preview version of Visual Studio and give it a try. 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. If you have any issues, please file an issue to our repo here. Comment below or reach us via email at visualcpp@microsoft.com or via Twitter at @VisualC. Sinem Akinci Program Manager II, Visual C++ Team Follow

Read More

MSVC Address Sanitizer – One DLL for all Runtime Configurations

MSVC Address Sanitizer – One DLL for all Runtime Configurations Amy Wishnousky August 10th, 20231 2 With Visual Studio 2022 version 17.7 Preview 3, we have refactored the MSVC Address Sanitizer (ASan) to depend on one runtime DLL regardless of the runtime configuration. This simplifies project onboarding and supports more scenarios, particularly for projects statically linked (/MT, /MTd) to the C Runtimes. However, static configurations have a new dependency on the ASan runtime DLL. Summary of the changes: ASan now works with /MT or /MTd built DLLs when the host EXE was not compiled with ASan. This includes Windows services, COM components, and plugins. Configuring your project with ASan is now simpler, since your project doesn’t need to uniformly specify the same runtime configuration (/MT, /MTd, /MD, /MDd). ASan workflows and pipelines for /MT or /MTd built projects will need to ensure the ASan DLL (clang_rt.asan_dynamic-.dll) is available on PATH. The names of the ASan .lib files needed by the linker have changed (the linker normally takes care of this if not manually specifying lib names via /INFERASANLIBS) You cannot mix ASan-compiled binaries from previous versions of the MSVC Address Sanitizer (this is always true, but especially true in this case). Motivation and Effects When building a project, you must specify how you want to link to the C and C++ Runtime Libraries. Using the /MD option will link dynamically to the runtimes and have DLL dependencies provided by the Windows operating system (ex: ucrtbase.dll) and the VC++ Redistributable (ex: vcruntime140.dll, msvcp140.dll). This is a great default option to choose, as it allows you to take advantage of the latest available on the system and minimizes memory usage. The /MT option specifies that the runtime libraries will be statically linked (via libucrt.lib, libvcruntime.lib, libcpmt.lib, etc) in your program such that you have no VC++ DLL dependencies. This option is useful when dynamically linking complicates deployment and is most frequently used with projects like plugins or drivers. Prior to Visual Studio 2022 v17.7 Preview 3, MSVC Address Sanitizer followed the same methodology and treated itself as one of the runtime libraries. If /MD was specified, ASan would be available as a DLL (clang_rt.asan_dynamic-.dll). If /MT was specified, ASan would be linked into the EXE. ASan can only have one instance at a time, so DLLs that were statically linked would assume the EXE of the process was also built with ASan and would call into that EXE for support. This caused problems for any software that could not recompile the host EXE with ASan enabled: plugins, COM components, Windows services, etc. Here is an example reported on Developer Community. Additionally, since each runtime configuration had its own version of ASan, you could not mix components with different runtime configurations. As of Visual Studio v17.7 Preview 3, runtime configuration is no longer considered and there is one Address Sanitizer DLL per architecture. In addition to supporting more scenarios, this can also significantly simplify onboarding a complex project with ASan. A side effect is that projects statically linked with /MT or /MTd now have a dependency on the ASan runtime DLL. Link Time Changes (/INFERASANLIBS) When /fsanitize=address is passed to the compiler, /INFERASANLIBS will automatically be passed to the linker, which will take care of the link time requirements for Address Sanitizer. If […]

Read More

Learn Beneficial Methods Of Modern C++

Hello everyone, Yilmaz here, from LearnCPlusPlus.org. Our educational LearnCPlusPlus.org web page is growing thanks to the support of you. We have many new readers, and we keep adding new C++ posts every day. These are good to learn the features of modern C++ compilers. In this round-up of recent articles, we explain some features of modern C++ along with other beneficial methods for intermediate and professional developers. This week, we have C++ examples and an explanation about using non-copyable movable types in C++ templates. The rvalue references are important and we explain rvalue references to eliminate unnecessary copying in C++. C++11, C++17, and C++20 standards added to the richness of the language, and we explain one of these additions – explicit conversion operators. If you wonder what an explicit specifier is, we explain them in another post. C++11 introduced two new features: defaulted and deleted functions, we can use these two keywords to delete or to default methods in C++, and we explain this in the other two posts. Table of Contents Where can I learn C++ with a free C++ compiler? How to use modern C++ with C++ Builder? How to learn C++ for free using C++ Builder? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional version of C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use modern C++ with C++ Builder? In C++, memory and CPU/GPU management are very important. Every declaration and usage of any items can cause a lot of heavy calculations, memory usage, and high CPU/GPU usage if used or manipulated unwisely. Using copy and move types in templates is very important when you develop a professional app. In the first post, we explain how you can use non-copyable movable types in C++ templates. https://learncplusplus.org/learn-how-to-use-non-copyable-movable-types-in-c-templates/ The rvalue references are a compound type like standard C++ references, which are referred to as lvalue references. New rvalue reference rules were set by the C++11 specifications. In many cases, data is copied that simply needs to be moved, that is, the original data holder need not retain the data. The rvalue reference can be used to distinguish cases that require copying versus cases that merely require moving data. In the next post, we explain how we use rvalue references to eliminate unnecessary copying in C++. https://learncplusplus.org/learn-how-to-eliminate-unnecessary-copying-in-c/ In modern C++, explicit-qualified conversion functions work in the same context as explicit-qualified constructors and produce diagnostics in the same contexts as constructors do. C++11, C++17, and C++20 standards have improvements in this explicit specifier. This is done to avoid situations when the compiler uncomplainingly accepts code […]

Read More

What Is A Virtual Function Specifier In Modern C++?

In modern C++ software, a virtual function is a function in a base class or struct that can be redefined in derived classes or structs. They are member functions whose behavior can be overridden in derived classes. The virtual function specifier is the ‘virtual’ specifier to declare a virtual function in a base class. In this post, we explain how we can use virtual function specifiers in modern C++. What is a virtual function specifier in modern C++? A virtual function is a function in a base class or struct that can be redefined in derived classes or structs. They are member functions whose behavior can be overridden in derived classes. The virtual function specifier is the ‘virtual‘ specifier to declare a virtual function in a base class. It is used by declaring the function prototype in the usual way and then prefixing the declaration with the virtual keyword. Here is the syntax of a virtual function:   virtual function_declaration;   To declare a pure function (which automatically declares an abstract class), prefix the prototype with the virtual keyword, and set the function equal to zero. Here is an example to virtual function and pure virtual function in C++:   virtual int funct1(void);       // A virtual function declaration.   virtual int funct2(void) = 0;   // A pure function declaration.   How to use virtual function specifier in modern C++? When you declare virtual functions, keep these guidelines in mind: They can be member functions only. They can be declared a friend of another class. They cannot be a static member. A virtual function does not need to be redefined in a derived class. You can supply one definition in the base class so that all calls will access the base function. To redefine a virtual function in any derived class, the number and type of arguments must be the same in the base class declaration and in the derived class declaration. The case for redefined virtual functions differing only in return type is discussed below. A redefined function is said to override the base class function. You can also declare the functions int Base::Fun(int) and int Derived::Fun(int) even when they are not virtual. In such a case, int Derived::Fun(int) is said to hide any other versions of Fun(int) that exist in any base classes. In addition, if class Derived defines other versions of Fun(), (that is, versions of Fun() with different signatures) such versions are said to be overloaded versions of Fun(). Is there a simple example of a virtual function in modern C++? Here is a simple example how you can use virtual function specifier in modern C++. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17   class Tx { virtual void myf() { std::cout

Read More

What Is An Explicit Virtual Override in Modern C++?

Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override specifiers. The override specifier is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. This override specifier is used with the C++ compiler that has C++11 and the other higher C++ standards. In this post, we explain an override specifier in modern C++. What Is the override specifier in C++? The override specifier (keyword) is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. In other words, it specifies that a method overrides a virtual method declared in one of its parent classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords: Final specifies that a method cannot be overridden, or a class cannot be derived. Override specifies that a method overrides a virtual method declared in one of its parent classes. The override specifier generally has two purposes, It shows that a given virtual method is overriding a virtual method of the base class. It indicates to the compiler that you are not adding or altering new methods that you think are overrides, and the compiler knows that is an override. In this post, we explain how to use the override specifier in C++. How to use the override specifier in C++? The override specifier is used to designate member functions that override a virtual function in a base class.   function_declaration override;   and, here is an example:   class Tbase { virtual void a(); };   class Tx : Tbase { void a() override; };   Is there a simple example to explicit virtual override specifier in C++ ? Here is a simple class example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   class Tbase { virtual void a(); void b(); virtual void c() final; virtual void d(); };   class Tx : Tbase { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, different return type };   int main() {     class Tx o1; }   Here is a simple struct example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   struct st_base { virtual void a(); void b(); virtual void c() final; virtual void d(); };   struct st_x : st_base { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, […]

Read More

What is a range-based for loop in modern C++?

In C++, the for loops are one of the great features of C and C++ language that has many options to break, continue, or iterate in a loop. In modern C++, there is a range-based for loop that makes it simple to iterate through a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard and in this post, we explain what is range-based for loop in examples. If you are new to programming and looking for a classic for() loop here is the post about it. What is range-based for loop in modern C++? In C++, for() function is used for loops, and they are one of the great features of C and C++ language and have many options to break or continue or iterate blocks of functionality. In modern C++, there is a range-based for loop that makes it simple to iterate trough a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard. Range-based for loop is a feature for the for() loops introduced by the C++11 standard. In Clang-enhanced C++ compilers, you can create for loops that iterate through a list or an array without computing the beginning, the end, or using an iterator. Here is the syntax for the range-based for loop in C++:   attr (optional) for ( init_statement (optional) range_declaration : range_expr) loop_statement;   or with loop body:   attr (optional) for ( init_statement (optional) range_declaration : range_expr) {    // loop_statements }   Is there a simple array example with a range-based for loop in C++? We can use range-based for loop to iterate through an array as below.   #include   int main() { int arr[] = { 00, 10, 20, 30, 40, 50 };   for (int a : arr) std::cout

Read More

Learn How To Use The Override Specifier In Modern C++

Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override specifiers. The override specifier is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. This override specifier is used with a C++ compiler that is compatible with C++11 and the other higher C++ standards. In this post, we explain an override specifier in modern C++. What Is the override specifier in C++? The override specifier (keyword) is used to redefine the base class function in a derived class with the same signature i.e. return type and parameters. In other words, it specifies that a method overrides a virtual method declared in one of its parent classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords: final specifies that a method cannot be overridden, or a class cannot be derived. override specifies that a method overrides a virtual method declared in one of its parent classes. The override specifier generally has two purposes, It shows that a given virtual method is overriding a virtual method of the base class. It indicates to the compiler that you are not adding or altering new methods that you think are overrides, and the compiler knows that is an override. In this post, we explain how to use the override specifier in C++. How to use the override specifier in C++? The override specifier is used to designate member functions that override a virtual function in a base class.   function_declaration override;   and here is an example:   class Tbase { virtual void a(); };   class Tx : Tbase { void a() override; };   Is there a simple example of how to use the override specifier in C++? Here is a simple class example about override and final specifiers that you can override a method of it, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   class Tbase { virtual void a(); void b(); virtual void c() final; virtual void d(); };   class Tx : Tbase { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int d() override; // error, different return type };   int main() {     class Tx o1; }   Here is a simple struct example about override and final specifiers that you can override a method of it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   #include   struct st_base { virtual void a(); void b(); virtual void c() final; virtual void d(); };   struct st_x : st_base { void a() override; // correct // void b() override; // error, an override can only be used for virtual functions // void c() override; // error, cannot override a function marked as final // int […]

Read More

How To Use A Move Constructor In Modern C++

Hello everyone. With the C++11 standards, the move constructor allows you to move the resources from one object to another object without copying them. In this post we have gathered together some recent C++ articles from LearnCPlusPlus.org about Move Constructors. All of the C++ examples in these posts can be used with C++ Builder Enterprise, Architect, Professional Editions, or the free version C++ Builder 11 CE Community Edition. Our standard C++ examples can also be used with Dev-C++, BCC C++ Compilers, and some other compilers such as the GCC compiler. If you just starting out on your C++ journey and want to jump to a modern IDE and C++ compiler, there is a free version of C++ Builder, C++ Builder 11 CE Community Edition released in April 2023. If you are a start-up developer, student, hobbyist, or just interested in learning to code then the C++ Builder Community Edition may well be just the thing for you. Table of Contents Where can I learn to code in C++ with a free C++ compiler? How to use a move constructor in C++ with C++ Builder CE? How to learn C++ for free with C++ Builder CE? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn to code in C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or the professional, full-featured C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use a move constructor in C++ with C++ Builder CE? The Move Constructor is one of the great features of Object Oriented Programming in C++. The move constructor allows you to move the resources from one object to another object without copying them. One of the move constructors is the Implicitly-defined Move Constructor which is defined or defaulted in a base class, and in the first post, we explain the Implicitly-defined Move Constructor in Modern C++. https://learncplusplus.org/what-is-an-implicitly-defined-move-constructor-in-modern-c/ Another move constructor is the Implicitly-declared Move Constructor, which is declared in a base class. In the next post we explain the implicitly-declared move constructor in Modern C++. https://learncplusplus.org/what-is-an-implicitly-declared-move-constructor-in-modern-c/ There is also an Eligible Move Constructor. In the next post, we explain an eligible move constructor in modern C++. https://learncplusplus.org/what-is-an-eligible-move-constructor-in-modern-c/ The other move constructor term is the Trivial Move Constructor which is defined or defaulted in a base class. We explain what a trivial move constructor in C++ is and how to use it. https://learncplusplus.org/what-is-a-trivial-move-constructor-in-modern-c/ Finally, the other move constructor is the Deleted Implicitly-declared Move Constructor (also it is shown in compiler errors as Implicitly-deleted Move Constructor) which is deleted in a base class directly or has been deleted because of some other declarations. In the last post, […]

Read More

Let’s Learn 5 Features Of Modern C++

Hello C++ Developers, I hope now you are enjoying your summer vacation, or you are happy with your work ???? Over on LearnCPLusPlus.org we add new C++ posts every day. These are good to learn the features of modern C++ compilers. In this round-up of recent articles we cover some important features of C++ such as the friend declaration, the inline namespace, the extended sizeof function, the assignment operator, and the rules of C++ such as the Rule of Zero, the Rule of Three, the Rule of Five, and the Rule of Six. Some of these topics may not be necessary for beginners but they are important to understand as you progress on your coding journey. Table of Contents Where can I learn C++ with a free C++ compiler? How to use a move constructor and copy assignment in C++ with C++ Builder CE? How to learn C++ for free using C++ Builder CE ? What is new in C++ Builder CE? What might be next for C++ Builder? Where can I learn C++ with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use a move constructor and copy assignment in C++ with C++ Builder CE? In C++ the standards have a wonderfully named extended friend declaration feature to address the class declarations which is a template parameter as a friend. This extended friend declaration is available with the C++ compiler that has C++11 and above standards. In the first post, we explain a friend and extended friend declaration in modern C++. https://learncplusplus.org/what-is-an-extended-friend-declaration-in-modern-c/ Namespaces are a kind of library or framework in C++. They are useful to hold the same name of classes, methods, and other entities in different namespaces. The C++11 standard and subsequent standards, allow the inline keyword in a namespace definition and in the next post, we explain how to use inline namespaces in modern C++. https://learncplusplus.org/what-is-the-inline-namespace-feature-in-modern-c/ Knowing the physical size of any data is very important in programming. While the programmers are trying to minimize data types or stream packages, the actual amount of global data transfer is increasing thanks to the demands of new technologies. To help combat this trend, we need to know each data type in programming. The sizeof() function is very useful to get the size of variables in bytes. C++11 extends the functionality of sizeof function so that class or class members can be sent as parameters even if no object has been instantiated. In another post, we explain with examples, what is the sizeof function, what is the extended sizeof() function, and how does C++11 extend the sizeof() operator on classes? https://learncplusplus.org/what-is-an-extended-sizeof-in-modern-c/ One of […]

Read More