background preloader

C++

Facebook Twitter

Modern C++ Design: Generic Programming and Design Patterns Applied - Andrei Alexandrescu. Multicast Delegates in C++11 | Code Strokes. C# has a wonderfully flexible delegate system capable of multicast events. This simple tool makes event driven software easier to write, and reduces coupling between objects. In 2003 Herb Sutter implemented a general form of the Observer pattern [1]. He called this the multi_function. It uses a mixture of TR1 and boost components to build a multi-cast delegate similar to C#'s. Fast-forward 9 years, and we now have variadic-templates thanks to C++11.

Variadic-Templates allow us to patch a missing component in Sutter's multi_function. Variadic Templates sound like a overwhelming cacophony. We are going to use the type expansion effects of variadic templates to consolidate some code. The etc is the point. Templates are designed to generate code at compile time to leverage source code reuse. Simpler, right? Variadic Templates are a powerful tool; there is certainly the potential to create some very obsucated code with this tool. Delegates: C++11 vs. Impossibly Fast - A Quick and Dirty Comparison.

History Jan 31, 2014 - Corrected code for Mikhail Semenov's approach. Introduction The following is for you who are curious about how Sergey Ryazanov's Impossibly Fast C++ Delegates compares to a C++11 lambda/function pointer approach. This is not meant to put one down, nor is it even close to comprehensive, which is why 'quick and dirty' is included in the title.

As a background my Windows API wrapper incorporated Ryazanov's delegates to implement the member function callbacks. My curiosity got the better of me, and I wanted to play around with the new C++11 lambda functions and see how delegates created with them compared to the existing approach in both usage and performance. Usage In order to create a usable callback in DWinLib 3.01 it had to have a function signature of: Hide Copy Code void DwlWinObjectDerivedClass::someFunction(DwlWinObject * usableObjectPointer) { ... } In C++11 using lamdba functions, it is fairly easy to overcome this limitation. DwlDelegate delegateC = del1; Results. Introduction à C++ 2011 (C++0x) Multicast Delegates in C++11 | Code Strokes. GotW.ca Home Page. Variadic Templates are Funadic | GoingNative 2012. C++11: std::function and std::bind | On C++ and other OOPscenities. Std::function and std::bind were born inside the Boost C++ Library, but they were incorporated into the new C++11 standard. std::function is a STL template class that provides a very convenient wrapper to a simple function, to a functor or to a lambda expression.

For example, if you want to store several functions, functors or lambda expressions in a vector, you could write something like this: As you can see, in this declaration: vector<function<void ()>> x; we are declaring a vector of functions. The new standard library also adds a new function: std::bind. std::bind is a template function that returns a std::function object that binds a set of arguments to a function. Consider the first code listing shown in this post, the functions stored in the vector do not receive any argument; but you probably want to store a function that receives more arguments in the same vector.

Say you have this function: How can you add it into the function of vectors of the first code listing? The output is: "How Do I?" Videos for Visual C++ C++ in Action: Industrial Strength Programming. Welcome Back to C++ (Modern C++) C++ is one of the most widely used programming languages in the world. Well-written C++ programs are fast and efficient. The language is more flexible than other languages because you can use it to create a wide range of apps—from fun and exciting games, to high-performance scientific software, to device drivers, embedded programs, and Windows client apps. For more than 20 years, C++ has been used to solve problems like these and many others. What you might not know is that an increasing number of C++ programmers have folded up the dowdy C-style programming of yesterday and have donned modern C++ instead.

One of the original requirements for C++ was backward compatibility with the C language. Since then, C++ has evolved through several iterations—C with Classes, then the original C++ language specification, and then the many subsequent enhancements. Because of this heritage, C++ is often referred to as a multi-paradigm programming language. Modern C++ emphasizes: Move Constructors. C++ Language and Standard Libraries. More C++ Idioms. Preface[edit] More Information[edit] Authors | Praise | Guidelines for Authors | GNU Free Documentation License Table of Contents[edit] Note: synonyms for each idiom are listed in parentheses.

Advanced idioms[edit] These are some more advanced C++ idioms. Envelope Letter TODO Deprecated idioms[edit] Const auto_ptr.