User-Defined Literals, Part 3 -- Andrzej Krzemieński

Andrzej Krzemieński’s latest:

User-defined literals — Part III

In the previous post we have seen how we can define a raw literal operator template that enables us to convert almost any binary literal of the form 11011_b to a corresponding value of type unsigned int at compile time and still use this value as a compile-time constant. However, the length of the literal has to be short enough to fit into the capacity of type unsigned int.

In this post, as promised, we will try to make our literal render values of different types based on the length of the binary literal, so that 11011_b renders value of type unsigned int and 100010001000100010001000100010001000_b renders value of type long long unsigned int ...

TC++PL4e: Coming soon, and preview cover shot

TC++PL43 cover - click to enlarge

Great news for C++ fans: Bjarne Stroustrup's authoritative book The C++ Programming Language has always been the book describing Standard C++. The latest news is that Bjarne is well along revising it for a new Fourth Edition that thoroughly covers the new C++11 standard.

There's no firm ship date at this time (sorry), but we hear that it's almost done and coming in a matter of months.

For now, here's a sneak peek at the near-final cover — enjoy.

Core C++, 5 of N: Explicit and Partial Specialization -- Stephan T. Lavavej

Core C++, 5 of N: Explicit and Partial Specialization -- Stephan T. Lavavej

Stephan T. Lavavej, aka STL, will take us on a journey of discovery within the exciting world of Core C++. We know lots of folks are either coming back to C++, coming to C++, or have never left C++. This lecture series, in n parts, is for all of you! Only STL can make that work (novice, intermediate, and advanced all bundled together and presented in a way only STL can do).

In Part 5, Stephan teaches us about Explicit and Partial Specialization of class and function templates.

From MSDN ->

Class templates can be specialized for specific types or values of the template arguments. Specialization allows template code to be customized for a specific argument type or value. Without specialization, the same code is generated for each type used in a template instantiation. In a specialization, when the specific types are used, the definition for the specialization is used instead of the original template definition. A specialization has the same name as the template of which it is a specialization. However, a template specialization can be different in many ways from the original template. For example, it can have different data members and member functions.

Use specialization to customize a template for a specific type or value. Use partial specialization when the template has more than one template argument and you only need to specialize one of them, or when you want to specialize behavior for an entire set of types, such as all pointer types, reference types, or array types.

// explicit_specialization1.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

// Template class declaration and definition
template <class T> class Formatter
{
   T* m_t;
public:
   Formatter(T* t) : m_t(t) { }
   void print()
   {
      cout << *m_t << endl;
   }
};

// Specialization of template class for type char*
template<> class Formatter<char*>
{
   char** m_t;
public:
   Formatter(char** t) : m_t(t) { }
   void print()
   {
      cout << "Char value: " << **m_t << endl;
   }
};

int main()
{
   int i = 157;
   // Use the generic template with int as the argument.
   Formatter<int>* formatter1 = new Formatter<int>(&i);

   char str[10] = "string1";
   char* str1 = str;
   // Use the specialized template.
   Formatter<char*>* formatter2 = new Formatter<char*>(&str1);

   formatter1->print();
   formatter2->print();
}

An Overview of the New C++ (C++11) -- Scott Meyers

An Overview of the New C++ (C++11)

Scott Meyers

Specification of the new version of C++ (“C++11”) is finally complete, and many compilers already offer a wealth of features from the revised language. And such features! auto-declared variables reduce typing drudgery and syntactic noise; Unicode, threading support, and alignment control address important functionality gaps; and rvalue references and variadic templates facilitate the creation of more efficient, more flexible libraries. The standard library gains resource-managing smart pointers, new containers, additional algorithms, support for regular expressions, and more. Altogether, C++11 offers much more than “old” C++. This intensively technical seminar introduces the most important new features in C++11 and explains how to get the most out of them.

Course Highlights

Participants will gain:

  • Knowledge of the most important C++11 features and how they help produce better programs.
  • Insights into how new features solve important problems.
  • Understanding of which features are useful primarily to library writers, which to class authors, and which to virtually all C++ developers.
  • Availability information regarding which features are available on which platforms.

Who Should Attend

Designers and developers who are using, considering using, or wish to know about the expanded capabilities of C++11. Attendees should be experienced with C++ and comfortable with its primary features (e.g., classes, templates, inheritance, STL, etc.). Familiarity with threading concepts (e.g., threads and mutexes) is helpful, but is not essential.

Format

Lecture and question/answer. There are no hands-on exercises, but participants are welcome – encouraged! – to bring computers to experiment with the material as it is presented.

Length

Three full days (six to seven lecture hours per day).

Detailed Topic Outline

The History and Vocabulary of C++ Evolution

Sample Program: C++98 vs. C++11

Features for Everybody:

  • auto for Type Declarations
  • Range-Based for Loops
  • >>” as Nested Template Closer
  • nullptr
  • Enhanced enums
  • Unicode characters and strings
  • Raw string literals
  • Uniform initialization syntax
  • Initializer lists
  • Lambda Expressions
  • Template Aliases
  • Threading Support
  • New Container Features
  • Smart Pointers (shared_ptr, weak_ptr, unique_ptr)
  • Hash Tables
  • Singly-Linked Lists
  • Fixed-Size Arrays
  • Tuples
  • Regular Expressions
  • Generalized Functors(function)
  • Generalized Binder (bind)
  • New Algorithms
  • Other New Library Functionality

Features Primarily for Class Authors: ◦Move Support, Rvalue References, and Perfect Forwarding

  • default Member Functions
  • delete Functions
  • Default Member Initialization
  • Delegating Constructors
  • Inheriting Constructors

Features Primarily for Library Authors: ◦Static Assertions

  • explicit Conversion Functions
  • Variadic Templates
  • decltype
  • Alignment control (i.e., alignof, alignas, etc.)

Yet More Features (Overview)

Removed and Deprecated Features (Overview)

Sources for Further Information

Fastware for C++--Scott Meyers

Fastware for C++

Scott Meyers

Fastware is software that's fast — that gets the job done quickly. Low latency is the name of the game, and achieving it calls for insights from software engineering, computer science, and the effective use of C++. This presentation addresses crucial issues in each of these areas, covering topics as diverse as CPU caches, speed-sensitive use of the STL, data structures supporting concurrency, profile-guided optimization, and more.

Much of the material in "Fastware for C++" is unique to this seminar, i.e., unavailable in Scott's publications or his other training courses. However, as the successor to Scott's acclaimed "High-Performance C++ Programming" seminar, "Fastware for C++" also includes updated discussions of topics from that course as well as from Scott's books, Effective C++, More Effective C++, and Effective STL.

Course Highlights

Participants will gain:

  • Recognition of the importance and implications of treating performance as a correctness criterion.
  • Understanding of how effective use of third-party APIs can improve system performance.
  • Knowledge of specific C++ practices that improve the speed of both the language and the STL.
  • Familiarity with concurrent data structures and algorithms poised to become de facto standards.

Who Should Attend

Systems designers, programmers, and technical managers involved in the design, implementation, and maintenance of performance-sensitive libraries and applications using C++. Participants should already know the basic features of C++ (e.g., classes, inheritance, virtual functions, templates), but expertise is not required. Knowledge of common threading constructs (e.g., threads, mutexes, condition variables, etc.) is helpful. People who have learned C++ recently, as well as people who have been programming in C++ for many years, will come away from this seminar with useful, practical, proven information.

Format

Lecture and question/answer. There are no hands-on exercises, but participants are welcome — encouraged! — to bring computers to experiment with the material as it is presented.

Length

Two full days (six to seven lecture hours per day).

Detailed Topic Outline

Treating speed as a correctness criterion.

  • Why "first make it right, then make it fast" is misguided.
  • Latency, initial and total.
  • Other performance measures.
  • Designing for speed.

Optimizing systems versus optimizing programs. ◦Most system components are "foreign."

  • Exercising indirect control over "foreign" components.
  • Examples.

CPU Caches and why they're important. ◦Data caches, instruction caches, TLBs.

  • Cache hierarchies, cache lines, prefetching, and traversal orders.
  • Cache coherency and false sharing.
  • Cache associativity.
  • Guidelines for effective cache usage.

Optimizing C++ usage: ◦Move semantics.

  • Avoiding unnecessary object creations.
  • When custom heap management can make sense.

Optimizing STL usage: ◦reserve and shrink_to_fit.

  • Range member functions.
  • Using function objects instead of function pointers.
  • Using sorted vectors instead of associative containers.
  • A comparison of STL sorting-related algorithms.

An overview of concurrent data structures. ◦Meaning of "concurrent data structure."

  • Use cases.
  • Common offerings in TBB and PPL.
  • Writing your own.

An overview of concurrent STL-like algorithms. ◦Thread management and exception-related issues.

  • Common offerings in TBB and PPL.
  • OpenMP.
  • Other TBB and PPL offerings.

Exploiting "free" concurrency.

  • Meaning of "free."
  • Multiple-approach problem solving.
  • Speculative execution.

Making use of PGO (profile-guided optimization) and WPO (whole-program optimization).

Resources for further information.

For more information on this course, contact Scott directly.

Adventures in Perfect Forwarding--Scott Meyers

From Scott Meyers' blog:

On Saturday, June 2, Facebook sponsored a one-day C++ conference and asked me (and others) to give a presentation.  I chose an abridged and updated version of a talk from C++ and Beyond 2011, "Adventures in Perfect Forwarding."  Judging by the dates on the comments below the video, it's been available since July, but I found out about it being online only today...

Read more at Scott's blog.

User-Defined Literals, Part 2--Andrzej Krzemieński

Andrzej Krzemieński's latest:

User-defined literals -- Part II

In the previous post on user-defined literals, we have seen what user-defined literalsare for and how you define a cooked literal operator, i.e., where compiler that sees literal 12_kg extracts value 12 of type long double and calls your function operator"" _kg(12.L) to transform the result.

In this post we will explore other aspects of user-defined literals: raw literal operators, which allow you to inspect every character in the literal...

C++11 Style: A Touch of Class -- Bjarne Stroustrup

C++11 Style: A Touch of Class -- Bjarne Stroustrup

How do we write good code in idiomatic C++11? What principles, techniques, and idioms can we exploit to make it easier to produce quality code? In this presentation, I make an argument for type-rich interfaces, compact data structures, integrated resource management and error handling, and highly-structured algorithmic code. I illustrate my ideas and guidelines with a few idiomatic code examples.

I use C++11 freely. Examples include auto, general constant expressions, uniform initialization, type aliases, type safe threading, and user-defined literals. This presentation reflects my thoughts on what "Modern C++" should mean in the 2010s: a language for programming based on light-weight abstraction with direct and efficient mapping to hardware, suitable for infrastructure code.

POCO 1.5.0 available

Development release 1.5.0 of POCO is now available. See the changelog or download here.

Major new features include:

  • a significantly improved Data framework
  • the new JSON library
  • lots of other improvements

Please note that this is a development release and not considered stable. Interfaces may change, backwards compatibility may be broken, not all platforms may work and there may be some rough edges.

C++ Rvalue References Explained--Thomas Becker

What are rvalue references and move semantics, and how do they work again, exactly?

If you haven't read Thomas Becker's nice explanation (or even if you have), be sure to check it out:

C++ Rvalue References Explained

Thomas Becker

1. Introduction
2. Move Semantics
3. Rvalue References
4. Forcing Move Semantics
5. Is an Rvalue Reference an Rvalue?
6. Move Semantics and Compiler Optimizations
7. Perfect Forwarding: The Problem
8. Perfect Forwarding: The Solution
9. Rvalue References and Exceptions
10. The Case of the Implicit Move
11. Acknowledgments and Further Reading