efficiency

CppCon 2015 Writing Great Libraries: 89 Easy Steps--Zach Laine

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Writing Great Libraries: 89 Easy Steps

by Zach Laine

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Writing code that does what you want it to do, correctly and efficiently, is hard. Doing so when you don't even know yet what you want the code to do yet is quite a bit harder. Yet this is the job of a library writer -- the users of a library may have a very different use case from that of the original author.

How do library writers develop correct and efficient APIs that are also:

- easy to use correctly - hard to use incorrectly - highly reusable - gracefully interoperable with other code

This talk gives lots of practical advice and techniques for accomplishing those goals and more.

CppCon 2015 C++: How I learned to stop worrying and love metaprogramming--Edouard Alligand

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

C++: How I learned to stop worrying and love metaprogramming

by Edouard Alligand

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Horrible software engineering technique conceived in the forge of Hell or the Only True Way of doing C++ in 2015, template metaprogramming and its cohort of companion techniques are sure to create animation in a group of programmers.

What if we were to tell you that an actual software product, actually sold to real customers and in production for now several years has been built on it? What if we were to tell you that a lot of advanced template techniques helped us to build a better software faster?

This talk is all about real life examples of template metaprogramming, why they are useful and when and how you could use them in your own projects.

CppCon 2015 Large Scale C++ with Modules: What You Should Know--Gabriel Dos Reis

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Large Scale C++ with Modules: What You Should Know

by Gabriel Dos Reis

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

“Modules” are a frequently requested and long-awaited feature by C++ programmers. In a nutshell, the idea is to have a direct language support for (a) expressing the boundaries and dependencies of a program component; (b) isolating source codes from macro vagaries; (c) scaling compile time, especially for large projects, given the ubiquity of “headers-only” template libraries; (d) spur innovation and deployment of semantics-aware developer tools.

This presentation will focus on three major points: (1) the design of the module proposal currently being considered by the C++ standards committee (design goals, properties, constraints); (2) implementations currently under way; and (3) early user experience and migration.

Modules directly address a problem (scalability) listed as one of the three major areas where C++17 is expected to significantly improve daily experience of the working C++ programmer. Naturally, this feature is also on the top ten list of C++17 functionalities Bjarne Stroustrup put forward in his “Thought on C++17.”

CppCon 2015 Functional Design Explained--David Sankel

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Functional Design Explained

by David Sankel

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

An oft-cited benefit of learning a functional language is that it changes one's approach to solving problems for the better. The functional approach has such a strict emphasis on simplistic and highly composable solutions that an otherwise varied landscape of solution possibilities narrows down to only a few novel options.

This talk introduces functional design and showcases its application to several real-world problems. It will briefly cover denotational semantics and several math-based programming abstractions. Finally, the talk will conclude with a comparison of functional solutions to the results more traditional design methodologies.

No prior knowledge of functional programming or functional programming languages is required for this talk. All the examples make use of the C++ programming language.

CppCon 2015 Simple, Extensible Pattern Matching in C++14--John R. Bandela

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Simple, Extensible Pattern Matching in C++14

by John R. Bandela

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

Recently, there has been in increased interest in applying functional programming techniques to C++. A very convenient construct that is used in functional programming languages is pattern matching. Due to the features introduced in C++11 and C++14 we can actually write a simple, easy to use, understandable, and extensible pattern matching library that is header only and uses no macros.

In this talks we will discuss the motivations for pattern matching, and see examples of use from other languages. We will then go on to design and implement a pattern matching library in C++. Particular attention will be payed to new C++14 features that greatly simplify the implementation such as integer_sequence, function return type deduction, and generic lambdas. The implementation will also serve a practical guide to using variadic templates and std::tuple.

We will show how the library can be easily extended and customized by working through some examples of customization such as working with user defined classes and structs, pointers, tuples, boost::optional, and boost::variant. At the end of the session, the audience will have an appreciation for pattern matching, as well as a sense that the C++11/14 features open new paradigms without sacrificing understandable code.

CppCon 2015 Time Programming Fundamentals--Greg Miller

Have you registered for CppCon 2016 in September? Don’t delay – Registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

Time Programming Fundamentals

by Greg Miller

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

"Time zones are logical and easy to use." -- No one ever

Time programming is notoriously difficult and error prone. Attempts at handling daylight-saving time, for example, often yield baffling code, which of course is explained by a similarly misguided comment. Programmer confusion can spread virally throughout the codebase when these misconceptions find their way into library interfaces.

The problem is not that dates and times are fundamentally complicated (though they are). The problem is the lack of a simplified mental model with library support. This would give programmers the concepts and vocabulary necessary to reason about and discuss these concepts, and the ability to express this reasoning in simple C++ terms.

In this talk I will show how date and time programming evolved into what it is today. I will present a greatly simplified mental model that applies to all programming languages. I will show clear examples using an open source C++ library that implements these simplified concepts. And I will present practical tips for proper time hygiene that should be used by everyone immediately.

Quick Q: Why doesn't std::queue::pop return value?

Quick A: To guarantee exception safety and for performance.

Some time ago on SO:

Why doesn't std::queue::pop return value?

So, whats the difference, pop function could have done the same thing.

It could indeed have done the same thing. The reason it didn't, is because a pop that returned the popped element is unsafe in the presence of exceptions (having to return by value and thus creating a copy).

Consider this scenario (with a naive/made up pop implementation, to ilustrate my point):

template<class T>
class queue {
    T* elements;
    std::size_t top_position;
    // stuff here
    T pop()
    {
        auto x = elements[top_position];
        // TODO: call destructor for elements[top_position] here
        --top_position;  // alter queue state here
        return x;        // calls T(const T&) which may throw
    }

If the copy constructor of T throws on return, you have already altered the state of the queue (top_position in my naive implementation) and the element is removed from the queue (and not returned). For all intents and purposes (no matter how you catch the exception in client code) the element at the top of the queue is lost.

This implementation is also inefficient in the case when you do not need the popped value (i.e. it creates a copy of the element that nobody will use).

This can be implemented safely and efficiently, with two separate operations (void pop and const T& front()).