Wednesday, August 17, 2011

Dynamic dispatching to template functions

As in the first post I wrote, less than a month ago, this time I am going to visit another stackoverflow question. The user has a template parametrized by a template argument and wants to write a non-templated function that takes an integer argument and will
dispatch the call to the appropriate template.

Wednesday, August 10, 2011

Value semantics: Copy elision

In the last post we discussed Named Return Value Optimization and one case of copy elision when constructing a variable from an rvalue expression.

But copy elision is not limited to those particular use cases. The question is where else can we take advantage of this optimization, and where the compiler will not be able to optimize away the extra copies.

Wednesday, August 3, 2011

Value semantics: NRVO

C++ is a language with value semantics. It was designed so that user defined types will behave in the same ways that primitive types do. This offers advantages, but also imposes burdens on development: programmers have to implement those semantics, and it also falls in the programmers to decide how parameters are passed in and out of functions and the impact that has.

With the language being designed with value semantics in mind, you would imagine that some optimizations are in place to avoid unneeded processing. In this category you can find things like [Named] Return Value Optimization, or copy elision in the current standard, or move semantics in the upcoming C++0x. Understanding what they mean, and when the compiler can or cannot apply them will improve efficiency and readability in the code.