Use CRTP for Polymorphic Chaining -- Marco Arena

Use CRTP for Polymorphic Chaining

by Marco Arena

 

This suggested video reminded me another use of CRTP I described some months ago. It was about how to support method chaining when inheritance is involved.

Consider this simple scenario:

struct Printer
{
   Printer(ostream& stream) : m_stream(stream) {}

   template<typename T>
   Printer& println(T&& message)
   {
      cout << message << endl;
      return *this;
   }

protected:
   ostream& m_stream;
};

struct CoutPrinter : public Printer
{
   CoutPrinter() : Printer(cout) {}

   CoutPrinter& color(Color c) 
   {
      // change console color
      return *this;
   }
}

//...

CoutPrinter printer;
printer.color(red).println("Hello").color( // OOPS

println("Hello") returns a Printer& and not a CoutPrinter&, so color is no longer available.

In this post I wrote a very simple approach based on CRTP to maintain the chaining relationship when inheritance is involved. I also propose a complicated-at-first-sight way to avoid duplication employing a bit of metaprogramming.

Continue reading...

Library WG split: Library and Library Evolution

The Committee page has been updated to reflect an organizational change: WG21 has decided to split the Library working group into a core “Library” group (LWG) and a forward-looking “Library Evolution” group (LEWG), following the model of how the language side is split between “core” and “evolution.”

Summary:

  • Library (LWG) is responsible for standard library quality and will handle non-evolutionary DR/issue processing and the wording/fine-tuning of new library facilities. Alisdair Meredith will continue to chair this group.
  • Library Evolution (LEWG) is responsible for processing and refining new proposals. Like EWG on the language side, Library Evolution may choose to handle the proposals directly or else recommend spinning up a Study Group. When a library-only SG is ready to progress, LEWG will determine whether to proceed, and if so the target ship vehicle (IS or TS). We are very grateful that Beman Dawes has agreed to serve as an interim “bridge” chair for a year or two, to apply his long experience with LWG to get Library Evolution set up and moving in the right direction, and to find and train a longer-term chair – so part of Beman’s job immediately will be to start to replace himself.

Thank you to Beman for his volunteering to lead Library Evolution, and to Alisdair for his continued excellent efforts leading Library! It’s hard work, and much appreciated.

Reminder: Bjarne Stroustrup live interview webcast on Monday

Reminder: On Monday morning in California (find your local time), the free online CodeRage 7 conference will kick off with a special inverview with Bjarne Stroustrup.

Note that registration is free but required -- you need to register in order to get information on how to access the live GoToWebcast event.

SPECIAL SESSION: A C++ Conversation with Bjarne Stroustrup

Bjarne Stroustrup and David Intersimone
 

Monday, December 10th - 8:00am - 8:45am PST (find your local time)

Bjarne Stroustrup will discuss the ISO C++11 standard, new language features, how C++11 builds on C++’s strengths, application portability, and C++’s ubiquitous presence in the markets.

 

Session Full?

We anticipate a very large audience for this special session.  In the event that the conference GoToWebinar session becomes full during this session, you may use the following link to listen to the audio stream:

AUDIO ONLY OVERFLOW - https://www3.gotomeeting.com/register/692238398

Shevlin Park: A C++ AMP Implementation in Clang/LLVM Using OpenCL -- Dillon Sharlet

At the November 2012 LLVM Developers' Meeting, Dillon Sharlet of Intel gave a presentation on Shevlin Park, a prototype implementation of C++ AMP in Clang/LLVM using OpenCL. In this talk Dillon briefly discusses C++ AMP in comparison to OpenCL, details the implementation and shows the results of performance analysis on the prototype implementation.

Shevlin Park: A C++ AMP implementation in Clang/LLVM using OpenCL (MP4 video) (PDF slides)

Dillon Sharlet - Intel

We describe “Shevlin Park,” a prototype implementation of Microsoft’s C++ AMP built on Clang, LLVM, and OpenCL.

We fully describe Shevlin Park’s implementation including how Clang/LLVM can be augmented to easily accommodate C++ AMP programming constructs, how C++ AMP computation can be expressed as OpenCL compute kernels, and finally how the C++ AMP runtime library can be easily implemented on an OpenCL runtime.

Using several benchmarks, we evaluate Shevlin Park’s performance, Microsoft’s DirectX based C++ AMP, and also conventional OpenCL.

Another Alternative to Lambda Move Capture -- John Bandela

[Ed.: John Bandela correctly points out some hazards of one approach to capture-by-move in C++11 lambdas. In this blog post, he offers his own safer solution.]

Another Alternative to Lambda Move Capture

by John Bandela

Today the post on isocpp.org called Learn How to Capture By Move caught my attention. I found the post informative and thought provoking, and you should go and read it before reading the rest of this post.

The problem is how do we lambda capture a large object we want to avoid copying?

The motivating example is below:

function<void()> CreateLambda()  
{  
  vector<HugeObject> hugeObj;  
  // ...preparation of hugeObj...  
  auto toReturn = [hugeObj] { /*...operate on hugeObj...*/ };  
  return toReturn;  
}

The solution proposed is a template class move_on_copy that is used like this:

auto moved = make_move_on_copy(move(hugeObj));
auto toExec = [moved] { /*...operate on moved.value...*/ };

However, there are problems with this approach, mainly in safety. The move_on_copy acts as auto_ptr and silently performs moves instead of copies (by design).

I present here a different take on the problem which accomplishes much of the above safely, however, with a little more verbosity in exchange for more clarity and safety.

Continue reading...

 

Modules update video available -- Doug Gregor

MP4 video of Doug Gregor's talk on Modules is now available via llvm.org. Combining links here:

Modules (MP4 video) (PDF slides)

Doug Gregor - Apple

The C preprocessor has long been a source of problems for programmers and tools alike.

Programmers must contend with widespread macro pollution and #include-ordering problems due to ill-behaved headers. Developers habitually employ various preprocessor workarounds, such as LONG_MACRO_PREFIXES, #include guards, and the occasional #undef of a library macro to mitigate these problems.

Tools, on the other hand, must cope with the inherent scalability problems associated with parsing the same headers repeatedly, because each different preprocessing context could effect how a header is interpreted – even though the programmer rarely wants it.

Modules seeks to solve this problem by isolating the interface of a particular library and compiling it (once) into an efficient, serialized representation that can be efficiently imported whenever that library is used, improving both the programmer’s experience and the scalability of the compilation process.

 

C++11 tempts OSS: KDevelop 4.6 IDE, KDE Frameworks 5, Qt 5.0 (Phoronix)

HT to Michael Larabel and Phoronix: It's great to see more projects moving to adopt modern C++11, from C++98/03 and even from C. Here are three fairly well-known projects in the "thinking about" stage of taking the plunge.

More Open-Source Projects Eyeing Up C++11

by Michael Larabel

KDE developers are currently contemplating the idea of allowing a subset of the C++11 language to be used within the KDevelop code-base. This C++11 change would happen for the KDevelop 4.6 integrated development environment release. Reasons are shared in this article for why one should consider using C++11 code.

[...]

Qt 5.0 is taking advantage of C++11 too, but there it's being handled in a backwards compatible manner so the code will still build as C++03 on older compilers. There is also more Qt C++11 developer information shared via slides from the recent Qt Developer Days event.

KDE Frameworks 5 may also use C++11.

Continue reading...

Tour of C++: Second chapter posted

This 2nd chapter of my Tour of C++ introduces the basic C++ abstraction mechanisms. If you have a 1990s view of C++, you are in for a few surprises: the integrated set of abstraction mechanisms in C++11 allows for simpler and more powerful abstractions than previously. For starters, pointers basically disappear from sight (hiding inside resource handles). Naturally, this is achieved without loss of performance. Enjoy!

Constructive comments would be most welcome.

Compile Time Computations -- Andrzej KrzemieĊ„ski

[Ed.: This is an old post but we feel it has valuable information about constexpr.]

This is a good article about constant expressions and computations at compile time. It gives good coverage of the new constexpr keyword for allowing compile-time and run-time evalution. constexpr is discussed rigorously with information on the rules required by the facility.

Compile-Time Computations

by Andrzej Krzemieński

[...] But now, consider the following example:

const int i = 2;

const char array[ i == 2 ? 64 : throw exception() ];

Throwing an exception (which is an expression) cannot be evaluated at compile-time, but since i == 2 is true, the third argument of conditional operator should not be evaluated; at least at run-time. So is the above a valid code? Not in C++03. It is valid however in C++11. Does this sound incredible? [...]

 

Continue reading...

 

Applied Informatics C++ Libraries and Tools Release 2012.1 now available

Release 2012.1 of Applied Informatics C++ Libraries and Tools is now available.

These libraries extend the POCO C++ Libraries with additional features, including:

  • Remoting for RPC/IPC and SOAP/WSDL web services, including SOAP 1.1 and 1.2
  • Open Service Platform for building modular, extensible applications
  • DNSSD/Zeroconf
  • Universal Plug and Play (UPnP)
  • Fast Infoset processing
  • Secure remote access to smart devices.

New in release 2012.1 are:

  • Support for C++ code generation from XML Schema and WSDL documents
  • Allowing Remoting to invoke SOAP 1.1/1.2 web services created using other middleware technologies such as Java JAX-WS or Microsoft .NET WCF
  • New Remoting featuers to support MTOM, HTTP Basic and Digest authentication and HTTP compression (gzip content encoding), as well as remote events with the new TCP transport

A free evaluation version is available for download.