CppCon 2018: Michael Price “Concepts and Contracts: When, What, and How”

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


Challenge: Write a library function template that takes exactly one positive, integral parameter.

There are many different ways to implement this function template in standard C++. The future additions of Concepts and Contracts will provide even more, possibly simpler, implementations as well. We will review the current status of both of these important new features, conduct a brief overview of what they can do, and then examine how they can work together towards an interface specification nirvana.

Michael Price, Synopsys, Inc.
Senior Software Engineer

Michael Price has developed and taught C++ for more than a decade and has been an active participant in WG21 since 2014, allocating most of his committee-time to EWG and the Reflection Study Group. He professes interest in making C++ "safer" to use through language and library design, automation, testing, and education. His past work includes enterprise-grade screen sharing applications, ABI-safe C++ component design, network protocol design, software development utilities, large-scale build and test automation, C/C++ compiler front-end development for static analysis tools, and a smidge of management experience.


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

I would say that unsigned char and signed char are integer types (as frequently I use int8_t and uint8_t which are commonly (un)signed char, as integers). Also wchar_t, char16_t, and char32_t are all integral, but they are probably not integer types.

connorhorman
Автор

There should be a way to check if an expression is defined. It would be false, if the compiler knows that it is undefined; otherwise it would return true:


static_assert(defined(1/0) == false);
static_assert(defined(1/1) == true);

int i;
// initialize i to any value including 0
[[assert: defined(1/i) == (i!=0)]];


In this case, defined is dependent on values at runtime and so it is calculated at runtime, the expression inside is never evaluated. There might be something like always_defined() to get a compile time constant:


int i;
int* p = &i;
int* q = (int*)malloc(sizeof(int)); // can be nullptr


// -> ERROR


This would also be used for checking if the iterators of a range are from the same container:


[[expects: defined(std::distance(first, last))]]; // the distance is only defined for two iterators from the same container.


It should not be a problem to be implemented in a compiler, because the compiler uses undefined behavior anyway to optimize the code.

cmdlp
Автор

His presentation material was disappeared from his Github repository.

gyuntist
Автор

Contracts should be checked at evaluation time, which means runtime or compiletime, everything else is unintuitive.

YourCRTube