Introduction to proposed std::expected - Niall Douglas - Meeting C++ 2017

preview_player
Показать описание

This talk introduces the very latest post-Toronto edition of the proposed std::expected T, E which may make it out of the WG21 Library Evolution Working Group (LEWG) next year in time for the C++ 20 standard.

Expected is a simple vocabulary type which can be used for functional programming, fixed latency failure handling, and can increase the clarity of expression of C++ program logic.

What problems can it solve? How do you use it? Why should you use it? What should you not use it for? All these questions will be answered, and more.
Рекомендации по теме
Комментарии
Автор

53:40

Sorry for also raining on your parade, Niall. : o )

The comment is correct, this is undefined behavior. As per temp.res (e.g. §14.6/8 in N3690): "If no valid specialization can be generated for a template, and that template is not instantiated, the template is ill-formed, no diagnostic required."

This implies UB if the compiler and linker make an executable anyway. Here's a possible but ugly workaround:

static_assert(std::is_same<T, file_handle>::value, "..."); // use any type that is OK

This makes the template well-formed.Never mind that it will never actually be instantiated for T = file_handle, because of the specialization. You could also use a type that is very unlikely to be used by accident, e.g. void(signed char *const volatile(&)[42]). Or better, define your own tag type, like BOZOSLIVEHERE.


Fortunately, this is very theoretical. Even if a compiler bothered to detect this case, which is really hard and in general not even possible (halting problem…), it would probably proudly display a diagnostic rather than silently creating a hard disk erasing, missile launching, nasal demons summoning, rogue program.

TruthNerds
Автор

The bool conversion operator might be confusing if you have a expected<bool, SomeErrorType>.

If i have a function expected<bool, FileSystemError> doesFileExist(string path), then i might accidentally write:

if (doesFileExist(myPath))
{
// Assume that file exists, when actually we just know that there was no FileSystemError
}

christophrcr
Автор

can't see small text on slides.
pls do slides on full screen, not camera view

SAS