August 2013

C++: Polymorphic Cloning and the CRTP (Curiously Recurring Template Pattern)--Katy Coe

katy.pngOn CRTP with multi-level inheritance, cloning, and the constructor forwarding problem.

C++: Polymorphic cloning and the CRTP (Curiously Recurring Template Pattern)

by Katy Coe

From the article:

A common problem in C++ occurs when you have an object of an unknown derived type and want to make a copy of it. ...

The solution is to use the commonly-used polymorphic cloning pattern. In this pattern, we define a virtual function -- which we’ll call clone() in this article -- which when called via an object pointer returns a new object of the correct derived type.

Continue reading...

XL C/C++ V11.1 supports extern templates, enum class, right angle brackets -- IBM C/C++ Cafe

[Ed.: We're pleased to report continued progress toward conformance by C++ compiler vendors. This post combines three features contributed by Cecilia, christineli, and Alice Ying]

The IBM XL C/C++ V11.1 now supports several new C++11 features.

With extern templates, you can provide an explicit instantiation declaration for a template specialization if an explicit instantiation definition of the template exists in other translation units or later in the same file. If one translation unit contains the explicit instantiation definition, other translation units can use the specialization without having the specialization instantiated multiple times.

With right angle brackets, you can avoid having to write an extra space in code like vector<list<int> >, and just write vector<list<int>> the way Stroustrup intended. 

With scoped enumerations, you can avoid the following problems with the traditional enumerations:

  • Implicit conversion to an integer
  • Inability to specify the underlying type
  • Scope issue: enumerator is injected into (a.k.a. "pollutes") the enclosing scope

Enjoy!

noexcept Destructors -- Andrzej Krzemieński

A slight change from C++98 to C++11, tightening up destructor semantics. Yes, it's still considered a best practice to never allow an exception to escape from a destructor -- in any language with destructor or Dispose functionality -- and now we have an additional reason in C++11:

noexcept Destructors

by Andrzej Krzemieński

From the article:

The goal of this post is to show one — fairly small — backwards incompatibility in C++11. It shows how noexcept exception specifications are implicitly generated for your destructors. In short, the following program used to run successfully in C++03 (under some definition of “success”): ...

In this post I do not intend to argue whether it is a bad practice or not to throw from destructors, but focus on what happens when you do. But I really do not encourage you to throw from destructors...

GotW #7a Solution: Minimizing Compile-Time Dependencies, Part 1—Herb Sutter

The solution to the latest GotW problem is now available. In this Item, the focus is on analyzing and managing compile-time dependencies.

    GotW #7a Solution: Minimizing Compile-Time Dependencies, Part 1

    by Herb Sutter

From the article:

Managing dependencies well is an essential part of writing solid code. C++ supports two powerful methods of abstraction: object-oriented programming and generic programming. Both of these are fundamentally tools to help manage dependencies, and therefore manage complexity. It’s telling that all of the common OO/generic buzzwords—including encapsulation, polymorphism, and type independence—along with most design patterns, are really about describing ways to manage complexity within a software system by managing the code’s interdependencies.

When we talk about dependencies, we usually think of run-time dependencies like class interactions. In this Item, we will focus instead on how to analyze and manage compile-time dependencies. As a first step, try to identify (and root out) unnecessary headers.

Guideline: Never #include unnecessary header files.

Guideline: Prefer to #include <iosfwd> when a forward declaration of a stream will suffice.

Guideline: Never #include a header when a forward declaration will suffice.

Continue reading...

Resumable Functions: async and await -- Jens Weller

JensWeller_small-da9313ea.jpgA look at resumable functions:

Resumable Functions: async and await

by Jens Weller

From the article:

While I did my series about the papers for Bristol, there was one paper, which I personally found a bit weird. This paper was about resumable functions, and at that time it was just another paper full of ideas for C++ to me. At C++Now suddenly, I got a better insight to what the use of resumable functions could be...

C++ String to Int -- Ivan Neeson

kumobius.PNGA nice comparison on converting a string to int:

C++ String to Int

by Ivan Neeson

From the article:

In this post I will compare the following methods for parsing a string into an integer in C++:

  • Manually
  • atoi()
  • strtol()
  • sscanf()
  • std::stoi (C++11 only)
  • std::istringstream
  • Boost.LexicalCast
  • Boost.LexicalCast with C locale
  • Boost.Spirit.Qi
  • Boost.Coerce

But first lets look at the requirements...

Overload 116 available

overload-116.PNGOverload 116 is now available. It contains the following articles, and more:

 

Overload 116

Auto -- A Necessary Evil? Part 2 -- Roger Orr

When is the use of auto good, and when is it evil?

Dynamic C++, Part 2 -- Alex Fabijanic

Alex Fabijanic and Richard Saunders continue to explore dynamic solutions in C++.

Portable String Literals in C++ -- Alf Steinbach

How hard can it be to make a file in C++ with international text literals in its name? Alf Steinbach shows us how to write a file called π.recipe.

Hard Upper Limit on Memory Latency -- Sergey Ignatchenko

How low can latency really get?

 

... and more!