C++ Meet-Up London: Ben Hanson on Lexertl

I attended the London C++ Meet-Up on Tuesday 13th August where Ben Hanson presented his work on Lexertl.

The next London C++ meet up is Wednesday, 11 September 2013 at Google Campus, 5 Bonhill St, London, England EC2A.

Leave a comment

Filed under C++, Programming

Bruce Dawson on a VS Compiler bug and heap tracing tools

Bruce Dawson found a compiler bug – and demonstrated a couple of good tools for tracing heap allocations.

Leave a comment

Filed under Programming

Scott Meyers Video – Universal References

Scott Meyers posted a video of his Universal References presentation from C++ and Beyond 2012. There’s also an article on the subject in the ACCU magazine Overload Issue 111, but I found the video much clearer (and more entertaining).

Meyers coined the term ‘Universal References’ for instances where the type of a variable or parameter is declared T&& and where T is a deduced type. It may look like an rvalue-reference, but it can also match lvalue-references:

20130813-122646.jpg

so it’s usually a mistake to overload on both T&& and const T& in a template function. Another key point is that, whereas an overload with an rvalue-reference would call std::move (e.g. a move constructor where T is not deduced), an overload with a universal-reference should call std::forward (because you don’t yet know if it’s an rvalue- or lvalue-reference):

20130813-123009.jpg

He also summarises the reference-collapsing rules as “lvalue-references are infectious”, a handy mnemonic from Stephan T. Lavavej.

1 Comment

Filed under C++, Programming, Video

Unusual solutions to avoid a breaking change

In the 1990’s and early 2000’s, I spent a lot of time travelling between London and Yorkshire. Much of that was spent driving on the A1. I recently repeated the journey and was taken aback at the lack of roundabouts (yes, this national 70 mph trunk road was regularly interrupted by roundabouts) and I missed Grantham Services, my usual halfway stop (being on a roundabout, it used to be convenient and hard to miss).

Happily, the services are still there, it’s just that they moved the motorway:

Grantham used to be built on the A1 southbound, with a roundabout to the north allowing northbound traffic to access it and a loop road back to the roundabout allowing them to get back. Recently the Highways Agency brought the A1 to the west of this interchange, with the old road becoming the southbound sliproads and a new bridge being constructed for northbound traffic.

In other related commuter news, London’s Docklands Light Railway recently solved a problem with short platforms by moving the station:

The station was constrained by sharp curves at both ends and could not therefore be further extended on its former site. The DLR’s plans to operate 3 car trains on this line therefore included the relocation of this station some distance to the east.

Incredibly, they built the new station and dismantled the old one without any disruption to the railway!

Leave a comment

Filed under Musing

std::sort now guarantees complexity O(N * log N) under C++11 Standard

I learnt recently that in the C++11 Standard, std::sort is required to have complexity O(N * log N) – not “on average N * log N with a worst case O(n^2)”, but guaranteed O(N * log N). This follows David Musser’s paper on Introspective Sorting and Selection.

Musser’s IntroSort uses QuickSort, but also monitors the number of partitions that have occurred. For example, one might use “median-of-3” to pick each pivot, but that could cause the O(N^2) behaviour if pathological sequence is encountered. In the paper, such a sequence is described by construction. IntroSort switches to HeapSort (which guarantees O(N * log N) performance) once the number of QuickSort partitions reaches 2 * log N. Musser proves that the overall algorithm performance remains O(N * log N).

As pointed out on StackOverflow, std::nth_element could be re-implemented with similar benefits:

Ideally exactly the same should apply std::nth_element but the ISO C++ 2011 standard has not tightened up the complexity requirements. So std::nth_element could be O(N * N) in the worst case.

Leave a comment

Filed under C++, Programming

Why call std::move on a variable that is already T&&?

A StackOverflow question asks why it is necessary to call std::move on a parameter when implementing a move constructor, given that the parameter is already declared T&&?

Named rvalue references are lvalues. Unnamed rvalue references are rvalues.

In other words, if you don’t explicitly move a into b X b = std::move(a); then you haven’t declared that you’ve finished with a, which is a named variable – so the compiler will call X’s copy constructor instead of X’s move constructor.

Leave a comment

Filed under C++, Programming

“Contextually converted to bool”

Chris Sharpe blogged an example of how marking operator bool() explicit neededn’t necessitate any overhead when using that type in a simple if condition:

An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5). The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.

Leave a comment

Filed under C++, Programming

Intelligent machines making their own decisions

Interesting article on LinkedIn about intelligent machines collaborating and communicating together to make decisions.

For example, sensors on your roof could sense that it’s a sunny day. The roof sensor is connected to the thermostat, and based on the data the thermostat receives, it automatically adjusts the inside temperature on the sunny side of the house and lowers the automatic shades before you do.

Imagine having all of the airplanes flying over every country wirelessly communicating to each other—not to all of the pilots, but the planes themselves communicating to each other real time flying conditions such as temperature, humidity, wind direction, speed, and turbulence. Each plane can then better anticipate potential issues and help the pilots respond before there is a problem.

Leave a comment

Filed under Technology

Optimizing C++ code (Visual C++ Team blog)

The Visual C++ Team blog has started a series on Optimizing C++ code.

  1. Introduction
  2. Overview
  3. Constant Folding
  4. Dead Code Elimination

Leave a comment

Filed under C++, Programming

Universal memorisation for C++

ISOCpp linked to a StackOverflow question about universal memorisation.

Leave a comment

Filed under C++, Programming