Articles & Books

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?

 

Quick Q: What's the difference between result_of and decltype? -- StackOverflow

From SO:

What's the difference between result_of<F(Args…> and decltype<f(args…)>?

I see that std::async is specified as follows:

template <class F, class... Args>                   // copied out of the standard
future<typename result_of<F(Args...)>::type>
async(F&& f, Args&&... args);

I had expected it to be declared like this:

template <class F, class... Args>
auto async(F&& f, Args&&... args) ->
  future<decltype(f(forward<Args>(args)...)>;

Would that be equivalent, or is there some way in which the use of result_of is preferable to the use of decltype? (I understand that result_of works with types, while decltype works with expressions.)

Preface for The C++ Programming Language 4th Ed. now available

The Preface for The C++ Programming Language, 4th Ed., is now available on InformIT and also appears in full on this site's Tour of C++ page.

Preface to TC++PL4e

All problems in computer science can be solved by another level of indirection, except for the problem of too many layers of indirection.

            -- David J. Wheeler

C++ feels like a new language. That is, I can express my ideas more clearly, more simply, and more directly in C++11 than I could in C++98. Furthermore, the resulting programs are better checked by the compiler and run faster. ...

The use of C++ has changed dramatically over the years and so has the language itself. From the point of view of a programmer, most of the changes have been improvements. The current ISO standard C++ (ISO/IEC 14882:2011, usually called C++11) is simply a far better tool for writing quality software than were previous versions. How is it a better tool? What kinds of programming styles and techniques does modern C++ support? What language and standard-library features support those techniques? What are the basic building blocks of elegant, correct, maintainable, and efficient C++ code? Those are the key questions answered by this book. Many answers are not the same as you would find with 1985, 1995, or 2005 vintage C++: progress happens.

Continue reading...