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

 

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

[Ed. Note: Although 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.]

As we gear up for the Bristol meeting that starts on April 15, the committee members now are busily reading and preparing positions on the many papers to be considered at the meeting.

Since our last meeting in Portland, we have a recent-record-level 160 technical papers, most of which are proposals that will need to be considered and discussed at the face-to-face meeting.

Those papers have been posted here; see the blog's Standardization category. Most recently, the papers have been posted individually with crafted excerpts, and many committee members have found that useful and already follow the mailings primarily here on the blog like everyone else.

However, it's not just the committee members who are interested in these proposals. The Meeting C++ blog has posted the first of a multi-part series of "digest" summaries of the papers in the March mailing (they skipped the January mailing). In this first installment, they summarize a first batch of papers...

A look at C++14: Papers Part I

This is the first Part of n, or lets say many entries in this blog. In total I hope to be able to cover most papers in 3-4 blog posts, giving the reader an overview over the suggestions and changes for C++ at the coming C++ Committee Meeting in April. In total there are 98 Papers, so I will skip some, but try to get as much covered as possible. I'll skip papers with Meeting Minutes for sure, and try to focus on those focusing on C++11 or C++14 features. As the papers are ordered by there Number (N3522 being the first), I'll go top down, so every blog post will contain different papers from different fields of C++ Standardization. As N352-24 are Reports about Active Issues, Defects and Closed Issues I'll skip them for now, also I will not read through Meeting Minutes and such.

Quick Q: So what's "lite" about "concepts lite" vs. full concepts?

There's a Q&A on StackOverflow, but see also the discussion about this in the March 12 Concepts conference call minutes that were posted here on the same day as the call. It's really more than minutes, it's also a record of discussion that answers this and other questions.

From SO:

What are the differences between concepts and template constraints?

I want to know what are the semantic differences between the C++ full concepts proposal and template constraints (for instance, constraints as appeared in Dlang or the new concepts-lite proposal for C++1y).

What are full-fledged concepts capable of doing than template constraints cannot do?

Quick Q: How should you use the standard smart pointers as members? -- StackOverflow

From StackOverflow:

Using smart pointers for class members

I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr/weak_ptr work in general. What I don't understand is the real usage. It seems like everybody recommends using unique_ptr as the way to go almost all the time. But how would I implement something like this: ...

Quick Q: Why does emplace_back need a (copy or) move constructor? -- StackOverflow

Quick A: It needs it when the container is a vector or similar, because the container may need to grow and reallocate which includes moving or copying the existing contents to the new location.

why does emplace_back need move constructor?

I have the following code... But the emplace_back doesn't use the move constructor. Why does the initialization require a move constructor in this instance?