数週間前、国際標準化委員会C ++の会議が開催されました。 その上、人々は(ほとんど)些細なことと交換せず、C ++ 20に向けていくつかの大きな一歩を踏み出しました。

速報:
- C ++ 20の拡張機能の概念!
- 範囲、ネットワーク、コルーチン/コルーチン:TSの形で実験としてリリースされました。
- モジュール:TSドラフトの準備ができました。
このすべてが何を意味するのか、コードの記述をどのように簡素化するのか、そして他に何があったのか-猫の下で読んでください。
コンセプト
Conceptsと呼ばれる素晴らしいものは、将来のC ++ 20標準のドラフトに含まれています。 これは、SFINAEイディオムを使用する汎用ライブラリの開発者にとって大きな喜びです。
SFINAE愛好家のやる気を起こさせる例ライブラリには関数 `* _fast`および` * _slow`があり、2つのテンプレートパラメーター `v`および` data`を入力として受け取ります。
- `* _fast`-高度に最適化されていますが、T&を返し有効にするために次の操作が必要です。
v += data;
v -= data;
v *= data;
v /= data;
- `*_slow` — , .
— `*_optimal`, `*_fast`, :
#include <iostream>
template <class Container, class Data>
void compute_vector_fast(Container& v, const Data& data) {
std::cout << "fast\n";
// ...
}
template <class Container, class Data>
void compute_vector_slow(Container& v, const Data& data) {
std::cout << "slow\n";
// ...
}
template <class Container, class Data>
void compute_vector_optimal(Container& v, const Data& data) {
// ??? call `compute_vector_slow(v, data)` or `compute_vector_fast(v, data)` ???
}
, , `std::enable_if_t` .
:
#include <iostream>
template <class T, class Data>
concept bool VectorOperations = requires(T& v, const Data& data) {
{ v += data } -> T&;
{ v -= data } -> T&;
{ v *= data } -> T&;
{ v /= data } -> T&;
};
template <class Container, class Data>
requires VectorOperations<Container, Data>
void compute_vector_optimal(Container& v, const Data& data) {
std::cout << "fast\n";
}
template <class Container, class Data>
void compute_vector_optimal(Container& v, const Data& data) {
std::cout << "slow\n";
}
:
GCC, -fconcepts, ,
.
proposal Concepts.
Ranges TS
Ranges . , C++20.
Ranges `sort(container)` `sort(container.begin(), container.end())`, namespace.
. , , , :
#include <vector>
#include <experimantal/ranges/algorithm>
namespace ranges = std::experimental::ranges;
int main () {
// get_some_values_and_delimiter() ,
// 42
std::vector<int> v2 = get_some_values_and_delimiter();
// 42 , :
auto it = ranges::find(v.begin(), ranges::unreachable{}, 42);
ranges::sort(++it, v.end());
}
.
SFINAE Ranges , : Sortable, Movable, Copyable, DefaultConstructible, Same…
,
.
Ranges.
Networking TS
, ( ), C++20. Networking TS ASIO.
:
- Networking TS move-only callback. ASIO
BOOST_ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS. unique_ptr, callback. - constexpr (, ip::address).
- : -callback allocator_type allocator_type get_allocator(); associated_allocator , get_allocator().
,
.
Networking.
Coroutines TS
— « , , ». .
@masterspline, MS, stackless. . — ( C++, ), , «». stackless , , , -, operatop()(), Duff's device switch() .
, Coroutines TS Networking TS.
+100 , 40 :
#include <ctime>
#include <iostream>
#include <string>
#include <experimental/net>
using net = std::experimental::net;
using net::ip::tcp;
std::string make_daytime_string() {
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
void start_accept(net::io_context& io_service) {
tcp::acceptor acceptor{io_service, tcp::endpoint(tcp::v4(), 13)};
while (1) {
tcp::socket socket(acceptor.get_io_service());
auto error = co_await acceptor.async_accept(socket, net::co_future);
if (error) break;
std::string message = make_daytime_string();
auto& [error, bytes] = co_await async_write(
socket, net::buffer(message), net::co_future
);
if (error) break;
}
}
int main() {
net::io_context io_service;
io_service.post([&io_service](){
try {
start_accept(io_service);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
});
io_service.run();
}
: Coroutines TS Networking TS . .
CLANG-6.0, -stdlib=libc++ -fcoroutines-ts, ,
.
Coroutines.
TS. !
?, cpp ( , ).
, , cpp. , cpp , . ( iostream , 20 — 30 cpp).
! — , . , ( , , ).
, . . , , . . , .
, — . ( inline/force_inline, 100 , — 99 ). , «» cpp.
, (`<windows.h>`, !) , ( std::string , multiple definitions, include guards ). ,
( ).
Modules.
, C++20
C++20 bitfields :
struct S {
unsigned x1:8 = 42;
unsigned x2:8 { 42 };
};
endianness :
if constexpr (std::endian::native == std::endian::big) {
// big endian
} else if constexpr (std::endian::native == std::endian::little) {
// little endian
} else {
// mixed endian
}
, C:
struct foo { int a; int b; int c; };
foo b{.a = 1, .b = 2};
:
auto bar = []<class... Args>(Args&&... args) {
return foo(std::forward<Args>(args)...);
};
:
- P0652R0 — . , , . .
- P0539R1 — integers, ( ) . ( , ), .
- P0639R0 — `constexpr_allocator` `constexpr_string + constexpr_vector`. , . — .
- P0415R0 — constexpr std::complex. LWG ( constexpr ). C++20.
constexpr?++
. constexpr- , constexpr — , .
, constexpr-:
[1],
[2].
, :
- P0457R0 — starts_with ends_with . . , , . proposal.
- P0458R0 — contains(key) member [unordered_]map/set/multimap/multiset. , LWG C++20.
, (, , Python):
fmt::format("The answer is {}", 42);
ring_span, boost::circular_buffer, ( view ).
. , « X?» «std::popcount(X)».
21 std::stacktrace (
Boost.Stacktrace),
std::shared_library .
C++20, C++17/14/11 C++ —
stdcpp.ru. !
? -
.