Articles & Books

Quick Q: When should I use noexcept? -- StackOverflow

As C++11-compliant compilers start to roll out and be adopted, people want to know to best use new C++11 features, such as:

When Should I Really Use noexcept?

 

  1. There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append noexcept to the function declaration in all such cases? ... For which situations should I be more careful about the use of noexcept, and for which situations can I get away with the implied noexcept(false)?
  2. When can I realistically except to observe a performance improvement after using noexcept... Do modern compilers take advantage of noexcept in this way? If not, can I excect some of them to do so in the near future?

 

Preconditions, Part 4 -- Andrzej KrzemieĊ„ski

Here is Andrzej's final (for now) post on preconditions, posted just 

Preconditions — Part IV

by Andrzej Krzemieński

This is the last post about preconditions. We will try to address concerns about a potential UB connected with expressing preconditions. We will also try to explore how language support for preconditions could look like.

 

...

Such a support for preconditions would be a very helpful feature. But let’s not fantasize too much. For now the best thing we can do is to use assertions and comments -- a very useful and often underestimated language feature.

Quick Q: What Is the Difference Between set and unordered_set in C++? -- StackOverflow

A common Q with a nice concise A:

What is the difference between set and unordered_set in C++?

Came across this good question, which is similar but not at all same since it talks about Java, which has different implementation of hash-tables, [...] So what is the difference in C++ implementation of set and unordered_set? This question can be ofcourse extended to map vs unordered_map and so on for other C++ containers.

Here is my initial assessment...

Quick Q: How Can Use a Lambda Function as a Hash Function for unordered_map? -- StackOverflow

Quick A: Name the lambda (by assigning it to a variable), then decltype it.

People sometimes ask this, so it's worth putting out a quick link to the short answer:

How to use lambda function as hash function in unordered_map?

I wonder if it is possible to use lambda function as custom hash function for unordered_map in C++11? If so, what is the syntax?

Quick Q: Why might a C++11 range-for loop appear slow? -- StackOverflow

Quick, can you spot the problem in this line of code?

for(vector<int> vec1 : backgroundData)

Probably you can -- but what's the best solution?

Read on here, yesterday on StackOverflow:

C++11: Why does this range loop decrease FPS by 35?

[...] Is the C++11 range-based loop so much slower than the old school for? I really want to hear an answer to this, because my eyes honestly prefer the range based loop, and I'd hate to find out that the range based loop is twice as slow.

 

Universal References (revisited) -- Ben Hekster

A response to Scott Meyers' recent article on Universal References, showing an analogy between &&-collapsing and const-collapsing, and making the counterargument that inventing a new concept may not be needed to clearly explain the standard. It's always interesting to see different experts' takes on how to understand and teach a  feature, particularly a new C++11 feature that we as a community are still absorbing.

Universal References

by Ben Hekster

... The ‘universal reference’ is not a concept you will see defined in the C++ standard, nor is it even something that has any conceptually objective existence in the language or compilation process. It is a construct defined by Meyers in an attempt to make some sense of behavior in the language that he presents as being unexpected or even mysterious. On closer inspection, however, I find that the observed mysterious behavior is actually quite readily explained and has an existing analog that corresponds to already intuitively-understood behavior. ...

Complex Initialization for a Const Variable -- Herb Sutter

How do you declare a const int variable when you have still have to do some computation to initialize it (so it shouldn't be const at first) but then want it be const after that?

Complex Initialization for a Const Variable

by Herb Sutter

 

On std-discussion, Shakti Misra asked:

> I have seen in a lot of places code like

int i;
if(someConditionIstrue)
{
    Do some operations and calculate the value of i;
    i = some calculated value;
}
use i; //Note this value is only used not changed. It should not be changed.

 

Olaf nailed it: The way to do it is with a lambda. ...

Ten C++11 Features Every C++ Developer Should Use -- Marius Bancila

codeproject.pngIgnoring the dangers of linking to items published on April 1, we offer:

Ten C++11 Features Every C++ Developer Should Use

by Marius Bancila

This article discusses a series of features new to C++11 that all developers should learn and use. There are lots of new additions to the language and the standard library, and this article barely scratches the surface. However, I believe some of these new features should become routine for all C++ developers. You could probably find many similar articles evangelizing different C++11 features. This is my attempt to assemble a list of C++ features that should be a norm nowadays. Table of contents:

  • auto
  • nullptr
  • Range-based for loops
  • Override and final
  • Strongly-typed enums
  • Smart pointers
  • Lambdas
  • non-member begin() and end()
  • static_assert and type traits
  • Move semantics

Quick Q: How to accept lambdas as callbacks? -- StackOverflow

The poster is definitely thinking along the right lines -- anything callable that would have accepted a pointer to function and/or functor in C++98 should be written to be able to accept a lambda function in modern C++.

So what about callbacks as a specific example?

Passing and storing lambda function as callbacks

I was wondering if this would be an accepted approach to writing callbacks:

Storing callbacks:

struct EventHolder {
    std::function<void()> Callback;
    EventTypes::EventType Type;
};
std::vector<Events::EventHolder> EventCallbacks;

Method definition:

void On(EventType OnEventType,std::function<void()>&& Callback)
{
    Events::EventHolder NewEvent;
    NewEvent.Callback=std::move(Callback);
    NewEvent.Type=OnEventType;
    EventCallbacks.push_back(std::move(NewEvent));
}

Binding event:

Button->On(EventType::Click,[]{
    // ... callback body
});

My biggest question would be regarding passing the Callback by value. Is this a valid approach?