News

Preconditions, Part 4 -- Andrzej Krzemieński

Here is Andrzej's final (for now) post on preconditions, posted just 

Preconditions — Part IV

by Andrzej Krzemieński

This is the last post about preconditions. We will try to address concerns about a potential UB connected with expressing preconditions. We will also try to explore how language support for preconditions could look like.

 

...

Such a support for preconditions would be a very helpful feature. But let’s not fantasize too much. For now the best thing we can do is to use assertions and comments -- a very useful and often underestimated language feature.

New adopted paper: N3659, Shared Locking in C++ -- Howard Hinnant, Detlef Vollmann, Hans Boehm

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3659

Date: 2013-04-19

Shared locking in C++ (a.k.a. reader/writer locks)

by Howard Hinnant, Detlef Vollmann, Hans Boehm

Excerpt:

N3568 was presented at the Spring 2013 meeting in Bristol to SG1 (Concurrency and Parallelism). The decision was to bring shared_mutex only forward for C++14. Also the specification should allow for spurious failures.

 

From N3568:

This proposal adds functionality to allow clients to easily code the well-known multiple-readers / single-writer locking pattern. ...

... a shared_mutex can be locked in one of two ownership modes:

  1. Exclusive, using lock(), or
  2. Shared, using lock_shared().

... The recommended pattern for locking a shared_mutex in shared ownership mode is not to call m.lock_shared() directly, but to use shared_lock<shared_mutex>:

shared_mutex mut;
...
void foo()
{
     shared_lock<shared_mutex> _(mut);
     // mut lock_shared'd here
     // ...
}    // mut.unlock_shared() called here

New adopted paper: N3672, std::optional (Revision 4) -- Fernando Cacciola, Andrzej Krzemieński

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3672

Date: 2013-04-19

A proposal to add a utility class to represent optional objects (Revision 4)

by Fernando Cacciola and Andrzej Krzemieński

Excerpt:

The basic usage of optional<T> can be illustrated with the following example.

optional<int> str2int(string);    // converts int to string if possible

int get_int_form_user()
{
  string s;

  for (;;) {
    cin >> s;
    optional<int> o = str2int(s); // 'o' may or may not contain an int
    if (o) {                      // does optional contain a value?
      return *o;                  // use the value
    }
  }
}

 

New adopted paper: N3642, User-defined Literals for Standard Library Types (pt1 v4) -- P. Sommerlad

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3642

Date: 2013-04-18

User-defi ned Literals for Standard Library Types (part 1 - version 4)

by Peter Sommerlad

This allows code like the following:

auto mystring = "hello world"s;   // type std::string

auto mytime = 42ns;               // type chrono::nanoseconds

 

New adopted paper: N3656, make_unique (Revision 1) -- Stephan T. Lavavej

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3656.txt

Date: 2013-04-18

make_unique (Revision 1)

by Stephan T. Lavavej

The paper includes source code for a complete implementation.

New adopted paper: N3649, Generic (Polymorphic) Lambda Expressions (R3) -- Vali, Sutter, Abrahams

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3649

Date: 2013-04-19

Generic (Polymorphic) Lambda Expressions (Revision 3)

by Faisal Vali, Herb Sutter, Dave Abrahams

Excerpt:

This document revises wording for generic lambda expressions as described in N3559: Proposal for Generic (Polymorphic) Lambda Expressions (Revision 2) and as approved by EWG and which should be referenced for further detail.  Today’s C++11 lambda expression concisely creates an instance of a class having a non-template function call operator.  We propose a pure extension of C++11 lambda syntax that creates an instance of a class having a function call operator template. 

Here follow some examples of the proposed syntax extension in use:

// 'Identity' is a lambda that accepts an argument of any type and
// returns the value of its parameter. 
auto Identity = [](auto a) { return a; };
int three = Identity(3);
char const* hello = Identity("hello");

// Conversion to function pointer for capture-less lambdas
int (*fpi)(int) = Identity;
char (*fpc)(char) = Identity;

New adopted paper: N3651, Variable Templates (Revision 1) -- Gabriel Dos Reis

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3651

Date: 2013-04-19

Variable Templates (Revision 1)

by Gabriel Dos Reis

Excerpt:

C++ has no notation for parameterized constants as direct as for functions or classes. For instance, we would like to represent the mathematical constant with precision dictated by a floating point datatype

template<typename T>
constexpr T pi = T(3.1415926535897932385);

and use it in generic functions, e.g. to compute the area of a circle with a given radius:

template<typename T>
T area_of_circle_with_radius(T r) {
    return pi<T> * r * r;
}

The types of variable templates are not restricted to just builtin types; they can
be user defined types. ...

This report proposes a simple extension to C++: allow variable templates. It makes definitions and uses of parameterized constants much simpler, leading to simplified and more uniform programming rules to teach and to remember.

New adopted paper: N3652, Relaxing Constraints on constexpr Functions -- Richard Smith

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3652

Date: 2013-04-18

Relaxing constraints on constexpr functions

by Richard Smith

This is a major extension to constexpr. Excerpt:

This paper describes the subset of N3597 selected for inclusion in C++14, relaxing a number of restrictions on constexpr functions. These changes all received overwhelmingly strong or unopposed support under review of the Evolution Working Group. It also incorporates Option 2 of N3598.

The changes selected by the Evolution Working Group were:

  • Allow declarations within constexpr functions, other than:
       static or thread_local variables
       uninitialized variables
  • Allow if and switch statements (but not goto)
  • Allow all looping statements: for (including range-based for), while, and do-while
  • Allow mutation of objects whose lifetime began within the constant expression evaluation.

In addition, in discussion of N3598, Option 2 was selected, which removes the rule that a constexpr non-static member function is implicitly const.

New adopted paper: N3648, Wording Changes for Generalized Lambda-Capture -- Vandevoorde, Voutilainen

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3648

Date: 2013-04-17

Wording Changes for Generalized Lambda-Capture

by Daveed Vandevoorde and Ville Voutilainen

NOTE: This paper is standardese only. For a description of the feature, see N3610, Generic Lambda-capture Initializers, Supporting Capture-by-move.

New adopted paper: N3638, Return Type Deduction for Normal Functions -- Jason Merrill

Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.

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: N3638

Date: 2013-04-17

Return type deduction for normal functions

by Jason Merrill

Excerpt:

Any C++ user introduced to the C++11 features of auto, lambdas, and trailing return types immediately wonders why they can't just write auto on their function declaration and have the return type deduced. This functionality was proposed previously in N2954, but dropped from C++11 due to time constraints, as the drafting didn't address various questions and concerns that the Core WG had. I have now implemented this functionality in GCC, and propose to add it to C++14. I discuss some of the less obvious aspects of the semantics below.

This proposal also resolves core DRs 975 (lambda return type deduction from multiple return statements), 1048 (inconsistency between auto and lambda return type deduction), and 1588 (deducing cv-qualified auto).

Note that this paper also allows lambdas (not just functions) with multiple return statements to have their return type deduced. Examples in the paper include multiple returns, recursion, and more:

auto iterate(int len)                        // return type is deduced as int
{
  for (int i = 0; i < len; ++i)
    if (search (i))
      return i;
  return -1;
}

auto sum(int i) {
  if (i == 1)
    return i;                                // return type deduced to int
  else
    return sum(i-1)+i;                       // ok to call it recursively now
}

template <class T> auto f(T t) { return t; } // return type deduced at instantiation time

[]()->auto& { return f(); }                  // return a reference