Interesting Papers at the Finland C++ Committee Meeting
Erich Keane
Erich.Keane@verizon.net
Introduction
- Based on the 2016-06 Mailing
- "Quick hits", not very in depth, but hopefully we can discuss those of interest
- Items chosen were ones that I found interesting, useful, or just odd!
- Not nearly a complete list, and most likely won't make the cut for C++17
- See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/#mailing2016-06
N4592: Modules Working Draft
- Intended Replacement for #include'ing files
- Heavily discussed, purpose is to reduce compile time, possibly get rid of cross-include macro shenanigans
- 2 Keywords, plus reusing export:
Gabriel Dos Reis
// Make names from std.io module available
import std.io;
// declare module M
module M;
// import & export names from std.random
export module std.random;
// Define & export Point
export struct Point { int x, y;}; // m1.ixx
module M;
export int sq(int i){return i*i;}
// m2.cxx
import M;
int main(){
return sq(9);
// sq imported from Module M
}P0057R4: Coroutines Wording
- Pairs closely with Futures/Promises
- Essentially creates Resumeable functions, adds keywords co_await, co_yield, co_return
- co_await: Suspends evaluation of current coroutine to wait for another coroutine
- co_yield: Suspends current function to allow caller to continue
- co_return: Used in co-routines instead of 'return', signals that the function is done (nothing left to return)
GGor Nishanov, Jens Maurer, Richard Smith, Daveed Vandevoorde
P0067R2: Elementary String Conversions
-
Add "to_chars", overloaded functions to convert numbers to strings.
- takes begin/end char*, T/float/double/long double value, format (scientific, fixed, hex), and precision
- returns to_chars_result, a char* and a bool if it overflowed the range
-
Add "from_chars", overloaded function to convert strings to numbers
- takes begin/end char*, T/float/double/long double ref value, and base
- returns from_chars_result, has const char*, error_code
Jens Maurer
P0084R1: Emplace Return Type
- Make emplace functions return a reference to the object!
-
Prevents common idiom of:
- my_container.emplace_back();
- my_container.back().do_something();
Alan Talbot
P0091R1: Template argument deduction for class templates
- Allow class template deduction!
Mike Spertus, Faisal Vali, Richard Smith
// Example Given:
lock_guard<shared_timed_mutex,
shared_lock<shared_timed_mutex>>
lock(mut, r1);
// Could be:
lock_guard lock(mut, r1);
Also:
template<class T>
struct Foo{ Foo(T t) {} };
//Currently would need to do:
Foo<int> f(5);
// Could be:
Foo f(5);
//If T is a lambda, really hard today!
auto lambda = [](int i){...};
Foo f<decltype(lambda)>(lambda);
// Could be:
Foo f([](int i) {...});
P00145R2: Refining Expression Evaluation Order for Idomatic C++
- Define evaluation order of parameters
Gabriel Dos Reis, Herb Sutter, Jonathan Caves
// Unspecified Behavior
int i = 0;
foo(i++, i++, i++, i++);
// Chaining is a problem! No gaurantee WHEN find is going to be executed here:
void f() {
std::string s = "but I have heard it works even if you don’t believe in it"
s.replace(0, 4, "").replace(s.find(“even”), 4, "only").replace(s.find(" don’t"), 6, "");
assert(s == "I have heard it works only if you believe in it");
}P0033R0: User Defined Literals for size_t
- Introduce 'zu' user-defined literal to the standard library, which creates a size_t.
-
Allows: auto s = 0zu;
- Particularly useful in for loops
- If size_type for something size_t, explicit size_t causes an error
Rein Halbersma
//Trivial Definition:
namespace std {
inline namespace literals {
inline namespace support_literals {
constexpr size_t operator "" zu(unsigned long long v){
return static_cast<size_t>(unsigned long v);
}
}
}
}P0373R0: Proposal of File LIterals
- Allow file-literals that load a file's content at compile time directly into a variable. Both Binary and Text in proposal:
Andrew Tomazos
// uses tF 'text file' literal to print contents of the .txt file
std::cout << tF"datafile.txt";
// uses the bF 'binary file' literal to fill variable f
constexpr auto f = bF"foo.bin";
P0380: A Contract Design
Gabriel Dos Reis, Jose Daniel Garcia, John Lakos, Alisdar Meredith, N. Myers, Bjarne Stroustrup
void push(queue &q)
//expects/ensures can also
// be on declarations
[[ expects: !q.full() ]]
[[ ensures: !q.empty() ]]
{
...
[[ assert: q.is_ok() ]];
}
- Contract checked at runtime based on build system settings
- Default action in program termination
- expects/ensures cannot access private members, assert can
- constexpr function contract cannot access non-local variables, but CAN access non-local constexpr variables
- Contract "levels": [[expects level: ...]]
- Default: runtime cost is small
- Audit: more comprehensive, expensive
- Axiom: for humans/analyzers only, not executed at run-time
fin
deck
By Erich Keane
deck
- 690