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. ...

A Look at C++14 and Beyond: Papers Part 3 -- Meeting C++

And part 3:

A look at C++14 and beyond: Papers Part 3

This is the 3rd part of my little series over the papers in the Pre-Bristol mailing. I have added "and beyond" to the title, as I decided to handle all papers with Part 2, and now will continue to do so. This edition will again feature a few highlights, and a lot of proposals from different areas of C++.

Also, please understand, that all of the papers here are proposals. None of them are voted into any standard yet, maybe Bristol will give us a hint about what to expect for C++14, maybe not. Still, the proposals will give an impression, on what C++14 could be like.

A Look at C++14: Papers Part 2 -- Meeting C++

A few days ago, we linked to Part 1. As noted there, although the article is labeled “A look at C++14,” not all of these papers are for C++14, and regardless of timeframe not all will be adopted. But it is a useful look at what’s on the commitee’s radar.

Part 2 has just been posted:

A look at C++14: Papers Part 2

This is the second part of my C++ Standardization Papers series. The first part has been received quite well, with more then 5k views in the first two days. Also isocpp.org, Phoronix, lwn.net, a lot of russian blogs and others have linked to it. There also was a nice discussion on reddit. Again, like in Part 1, I want to emphasize, that I only cover part of all papers in this blog post. Also not all of these papers are meant to be happening with C++14, modules & concepts for example are not going to be part of C++14 (at least this is highly unlikely). Still, I will cover those papers too, as some of them will be discussed in Bristol for sure. All papers can be found here.

Some words on C++14. C++14 is not going to be like C++11 changing the language a lot. Its more meant to enhance the language with libraries, and improve or provide bug fixes for C++11. That's why you could call C++14 a minor Standard, and the next major C++ Standard is C++17, at least you could see this as the current plan and road map for C++. But lets have a look at the papers: ...

 

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...