Articles & Books

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?

New paper: N3580, Concepts Lite -- Andrew Sutton, Bjarne Stroustrup, Gabriel Dos Reis

[Ed.: We're calling particular attention to this paper as of broad interest to the community, although still undergoing standardization. This is one of the major papers being considered at the upcoming Bristol standards meeting in April for near-term standardization work.]

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N3580

Date: 2013-03-17

Concepts Lite: Constraining Templates with Predicates

by Andrew Sutton, Bjarne Stroustrup, Gabriel Dos Reis

Excerpt:

In this paper, we introduce template constraints (a.k.a., “concepts lite”), an extension of C++ that allows the use of predicates to constrain template arguments. The proposed feature is minimal, principled, and uncomplicated. Template constraints are applied to enforce the correctness of template use, not the correctness of template definitions. The design of these features is intended to support easy and incremental adoption by users. More precisely, constraints:

  • allow programmers to directly state the requirements of a set of template arguments as part of a template’s interface,
  • support function overloading and class template specialization based on constraints,
  • fundamentally improve diagnostics by checking template arguments in terms of stated intent at the point of use, and
  • do all of this without any runtime overhead or longer compilation times.

This work is implemented as a branch of GCC-4.8 and is available for download at http://concepts.axiomatics.org/˜ans/. The implementation includes a compiler and a modified standard library that includes constraints. Note that, as of the time of writing, all major features described in this report have been implemented.

This paper is organized like this:

  • Tutorial: introduces the basic notions of constraints, shows examples of their use, and gives examples of how to define constraints.
  • Discussion: explains what constrains are not. In particular, we try to outline constraints’s relation to concepts and to dispel some common misconceptions about concepts.
  • User’s guide: provides many more tutorial examples and demonstrate the completeness of the constraints mechanism.
  • Implementation: gives an overview of our GCC compiler support for constraints.
  • Extensions: we discuss how constraints might be extended to interact with other proposed features.
  • Language definition: presents a semi-formal definition of constraints.

Quick Q: Why use C varargs when you have initializer_lists and variadic templates? -- StackOverflow

Quick A: No reason, varargs are type-unsafe and entirely superseded by C++11 features (unless you need C compatibility).

Why use variadic arguments now when initializer lists are available?

I've been wondering what are the advantages of variadic arguments over initializer lists. Both offer the same ability -- to pass indefinite number of arguments to a function.

 

What I personally think is initializer lists are a little more elegant. Syntax is less awkward.

Also, it appears that initializer lists have significantly better performance as the number of arguments grows.

 

So what am I missing, besides the possibility to use use variadic arguments in C as well?

Preconditions, Part 3 -- Andrzej Krzemieński

Andrzej continues with a third installment in his series on preconditions.

Preconditions, Part 3

by Andrzej Krzemieński

In this post, I examine a couple of cases and try to answer the question when and how to specify preconditions, and when it is better not to do it. I believe it gives a deeper insight into the nature of preconditions.

New paper: N3551, Random Number Generation in C++11 -- Walter Brown

Ed.: Most standardization papers are about technical changes. It's not often you get a tutorial written by a world-class expert submitted as a WG21 paper. Thanks, Walter!

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N3551

Date: 2013-03-12

Random Number Generation in C++11

by Walter Brown

Excerpt:

For programmers seeking to familiarize themselves with the <random> component of the
C++11 standard library, we provide background information and tutorial guidance with numerous
usage examples.

1 Introduction 1
2 Getting started 2
3 An anti-pattern 3
4 Initializing an engine 3
5 What else can an engine do? 4
6 Engines in the standard library 5
7 Sharing an engine 5
8 Distributions in the standard library 6
9 What else can a distribution do? 8
10 A simple toolkit 9
11 A final example 9
12 Caveat lector! 10
13 What’s next? 11
14 Acknowledgments 11
15 Bibliography 11
16 Revision history 12