• Keine Ergebnisse gefunden

Contracts programming for C++20

N/A
N/A
Protected

Academic year: 2022

Aktie "Contracts programming for C++20"

Copied!
169
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Contracts programming for C++20

Contracts programming for C++20

Current proposal status

J. Daniel Garcia

ARCOS Group University Carlos III of Madrid

Spain

April, 28th, 2017

(2)

Contracts programming for C++20

Warning

c

This work is under Attribution-NonCommercial- NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license.

You arefreetoShare— copy and redistribute the ma- terial in any medium or format.

b

You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

e

You may not use the material for commercial purposes.

d

If you remix, transform, or build upon the material, you

(3)

Contracts programming for C++20

Download this slides

Download the slides:

https://www.arcos.inf.uc3m.es/jdgarcia/

others/talks-and-seminars/

(4)

Contracts programming for C++20

Who am I?

A C++ programmer.

Started writing C++ code in 1989.

A university professor in Computer Architecture. A ISO C++ language standards committee member. My goal: Improve applications programming.

Performancefasterapplications.

Energy efficiencybetterperformance per Watt. Maintainabilityeasierto modify.

Reliabilitysafercomponents. More at:

https://www.arcos.inf.uc3m.es/jdgarcia.

(5)

Contracts programming for C++20

Who am I?

A C++ programmer.

Started writing C++ code in 1989.

A university professor in Computer Architecture.

A ISO C++ language standards committee member. My goal: Improve applications programming.

Performancefasterapplications.

Energy efficiencybetterperformance per Watt. Maintainabilityeasierto modify.

Reliabilitysafercomponents. More at:

https://www.arcos.inf.uc3m.es/jdgarcia.

(6)

Contracts programming for C++20

Who am I?

A C++ programmer.

Started writing C++ code in 1989.

A university professor in Computer Architecture.

A ISO C++ language standards committee member.

My goal: Improve applications programming. Performancefasterapplications.

Energy efficiencybetterperformance per Watt. Maintainabilityeasierto modify.

Reliabilitysafercomponents. More at:

https://www.arcos.inf.uc3m.es/jdgarcia.

(7)

Contracts programming for C++20

Who am I?

A C++ programmer.

Started writing C++ code in 1989.

A university professor in Computer Architecture.

A ISO C++ language standards committee member.

My goal: Improve applications programming.

Performancefasterapplications.

Energy efficiencybetterperformance per Watt.

Maintainabilityeasierto modify.

Reliabilitysafercomponents.

More at:

https://www.arcos.inf.uc3m.es/jdgarcia.

(8)

Contracts programming for C++20

ARCOS@uc3m

UC3M: A young, international, research oriented university.

ARCOS: Applied research group:

Lines: High Performance Computing, Big data, Cyberphisical Systems, andProgramming Models for Application Improvement

Improving Applications:

REPARA: Reengineering and Enabling Performance and poweR of Applications. Funded by EU (FP7).

RePhrase: REfactoring Parallel Heterogeneous Resource Aware Applications. Funded by EU (H2020).

(9)

Contracts programming for C++20 A brief history of contracts

1 A brief history of contracts 2 Introduction

3 Contracts in C++

4 Contract checking 5 Contracts on interfaces 6 Final notes

(10)

Contracts programming for C++20 A brief history of contracts

Why correctness?

If it doesn’t have to produce correct results, I can make it arbitrarily fast.

Gerald M. Weinberg

Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.

Bertrand Meyer

(11)

Contracts programming for C++20 A brief history of contracts

Why correctness?

If it doesn’t have to produce correct results, I can make it arbitrarily fast.

Gerald M. Weinberg

Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.

Bertrand Meyer

(12)

Contracts programming for C++20 A brief history of contracts

Why correctness?

If it doesn’t have to produce correct results, I can make it arbitrarily fast.

Gerald M. Weinberg

Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.

Bertrand Meyer

(13)

Contracts programming for C++20 A brief history of contracts

Why are we here?

Because we are concerned about writing correct software.

Isn’t a library solution enough? We already tried that!

Compilers and static analyzers do not understand that approach.

What did others do?

Several language solutions out there (D, Ada, C#).

(14)

Contracts programming for C++20 A brief history of contracts

Why are we here?

Because we are concerned about writing correct software.

Isn’t a library solution enough?

We already tried that!

Compilers and static analyzers do not understand that approach.

What did others do?

Several language solutions out there (D, Ada, C#).

(15)

Contracts programming for C++20 A brief history of contracts

Why are we here?

Because we are concerned about writing correct software.

Isn’t a library solution enough?

We already tried that!

Compilers and static analyzers do not understand that approach.

What did others do?

Several language solutions out there (D, Ada, C#).

(16)

Contracts programming for C++20 A brief history of contracts

Why are we here?

Because we are concerned about writing correct software.

Isn’t a library solution enough?

We already tried that!

Compilers and static analyzers do not understand that approach.

What did others do?

Several language solutions out there (D, Ada, C#).

(17)

Contracts programming for C++20 A brief history of contracts

Why are we here?

Because we are concerned about writing correct software.

Isn’t a library solution enough?

We already tried that!

Compilers and static analyzers do not understand that approach.

What did others do?

Several language solutions out there (D, Ada, C#).

(18)

Contracts programming for C++20 A brief history of contracts

Contracts in C++

First proposal for contracts programming in 2005.

N1613: Proposal to add Design by Contract to C++.

Throsten Ottosen.

Died during the C++0x process.

Next attempt in 2013.

N3604: Centralized Defensive-Programming Support for Narrow Contracts. John Lakos, Alexei Zakharov.

(19)

Contracts programming for C++20 A brief history of contracts

Contracts in C++

First proposal for contracts programming in 2005.

N1613: Proposal to add Design by Contract to C++.

Throsten Ottosen.

Died during the C++0x process.

Next attempt in 2013.

N3604: Centralized Defensive-Programming Support for Narrow Contracts. John Lakos, Alexei Zakharov.

(20)

Contracts programming for C++20 A brief history of contracts

Current contracts effort

2014-2015:Multiple proposals on contracts programming.

Discussions in the standards committee.

2016: Joint proposal trying to consider trade-offs. Gabriel Dos Reis, J. Daniel Garcia, John Lakos, Alisdair Meredith, Nathan Myers, Bjarne Stroustrup.

Many others provided feedback and ideas. Targeting C++20!.

(21)

Contracts programming for C++20 A brief history of contracts

Current contracts effort

2014-2015:Multiple proposals on contracts programming.

Discussions in the standards committee.

2016: Joint proposal trying to consider trade-offs.

Gabriel Dos Reis, J. Daniel Garcia, John Lakos, Alisdair Meredith, Nathan Myers, Bjarne Stroustrup.

Many others provided feedback and ideas.

Targeting C++20!.

(22)

Contracts programming for C++20 Introduction

1 A brief history of contracts 2 Introduction

3 Contracts in C++

4 Contract checking 5 Contracts on interfaces 6 Final notes

(23)

Contracts programming for C++20 Introduction

Correctness and Robustness

In the design of a library two related properties need to be considered:robustnessandcorrectness.

CorrectnessDegree to which a software component matches its specification.

RobustnessAbility of a software component to react appropriately to abnormal conditions.

Today many libraries use a single feature for managing both properties: exception handling.

(24)

Contracts programming for C++20 Introduction

Correctness and Robustness

In the design of a library two related properties need to be considered:robustnessandcorrectness.

CorrectnessDegree to which a software component matches its specification.

RobustnessAbility of a software component to react appropriately to abnormal conditions.

Today many libraries use a single feature for managing both properties: exception handling.

(25)

Contracts programming for C++20 Introduction

Correctness and Robustness

In the design of a library two related properties need to be considered:robustnessandcorrectness.

CorrectnessDegree to which a software component matches its specification.

RobustnessAbility of a software component to react appropriately to abnormal conditions.

Today many libraries use a single feature for managing both properties: exception handling.

(26)

Contracts programming for C++20 Introduction

Correctness and Robustness

In the design of a library two related properties need to be considered:robustnessandcorrectness.

CorrectnessDegree to which a software component matches its specification.

RobustnessAbility of a software component to react appropriately to abnormal conditions.

Today many libraries use a single feature for managing

(27)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled. We decouple error identification from error handling. Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation. What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(28)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling. Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation. What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(29)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation. What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(30)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation. What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(31)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation. What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(32)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(33)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality. Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(34)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality.

Document. Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(35)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality.

Document.

Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(36)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality.

Document.

Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(37)

Contracts programming for C++20 Introduction

Exceptions in use

When afailurehappens, we use exceptions as an error reporting mechanism.

Notify that an error has occurred and needs to be handled.

We decouple error identification from error handling.

Example: Throwingbad_alloc.

When library detects anassumption was not met, it needs a mechanism to react.

Assumption not metcontract violation.

What do we do on contract violations today?

Ignore reality.

Document.

Throw exceptions.

Robustnessandcorrectnessare orthogonal properties andshould be managed independently.

(38)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs. Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(39)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(40)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(41)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown. Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(42)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(43)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(44)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify. T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(45)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify. T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(46)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_alloc if storage cannot be obtained.

(47)

Contracts programming for C++20 Introduction

Robustness in the C++ standard library

Robustness: Identification and handling of abnormal situations.

Those situations occur in completely correct programs.

Example: Failure to allocate memory.

You might eventually recover from a robustness issue.

Or at least gracefully shutdown.

Is end of file a robustness issue?

The C++ standard library identifies those cases by specifying

i the condition firing the situation.

ii the exception that will be thrown to notify.

T * allocator<T>::allocate(std::size_t n);

Throws:bad_allocif storage cannot be obtained.

(48)

Contracts programming for C++20 Introduction

Correctness and contracts

Correctness→Finding programming errors.

Yes! Sometimes we write incorrect software.

Who’s guilty?

A contract violation happens because:

A caller does not fulfil the expectations before calling a function.

A callee does not fulfill what should be ensured after its own execution.

A key difference:

A program failure is usually due to external conditions and cannot be avoided.

A contract violationshould never happen in a correct program.

(49)

Contracts programming for C++20 Introduction

Correctness and contracts

Correctness→Finding programming errors.

Yes! Sometimes we write incorrect software.

Who’s guilty?

A contract violation happens because:

A caller does not fulfil the expectations before calling a function.

A callee does not fulfill what should be ensured after its own execution.

A key difference:

A program failure is usually due to external conditions and cannot be avoided.

A contract violationshould never happen in a correct program.

(50)

Contracts programming for C++20 Introduction

Correctness and contracts

Correctness→Finding programming errors.

Yes! Sometimes we write incorrect software.

Who’s guilty?

A contract violation happens because:

A caller does not fulfil the expectations before calling a function.

A callee does not fulfill what should be ensured after its own execution.

A key difference:

A program failure is usually due to external conditions and

(51)

Contracts programming for C++20 Introduction

Correctness in the C++ standard library

From the standard:

Violation of the preconditions specified in a function’sRequires:

paragraph results in undefined behavior unless the functions Throws: paragraph specifies throwing an exception when the precondition is violated.

In practice, there are two approaches in the standard library:

Do nothingUndefined behaviour. NotifyThrow an exception.

(52)

Contracts programming for C++20 Introduction

Correctness in the C++ standard library

From the standard:

Violation of the preconditions specified in a function’sRequires:

paragraph results in undefined behavior unless the functions Throws: paragraph specifies throwing an exception when the precondition is violated.

In practice, there are two approaches in the standard library:

Do nothingUndefined behaviour.

(53)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code? Should we?

Can we make our semantics available to external tools? Can we avoid the comment/code synch issue?

Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(54)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code?

Should we?

Can we make our semantics available to external tools? Can we avoid the comment/code synch issue?

Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(55)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code?

Should we?

Can we make our semantics available to external tools?

Can we avoid the comment/code synch issue? Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(56)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code?

Should we?

Can we make our semantics available to external tools?

Can we avoid the comment/code synch issue?

Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(57)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code?

Should we?

Can we make our semantics available to external tools?

Can we avoid the comment/code synch issue?

Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(58)

Contracts programming for C++20 Introduction

Can we do it better?

Can we do more than just run-time checks?

Can we use contracts information for optimizing-out code?

Should we?

Can we make our semantics available to external tools?

Can we avoid the comment/code synch issue?

Can we learn from experiences in other programming languages?

Can we serve different communities with different needs?

(59)

Contracts programming for C++20 Contracts in C++

1 A brief history of contracts 2 Introduction

3 Contracts in C++

4 Contract checking 5 Contracts on interfaces 6 Final notes

(60)

Contracts programming for C++20 Contracts in C++

What is a contract?

A contract is the set ofpreconditions,postconditions andassertionsassociated to a function.

Precondition: What are theexpectationsof the function? Postconditions: What must the functionensureupon termination?

Assertions: What predicates must be satisfied in specific locations of a function body?

It statesrightsandobligationsofclientandsupplier.

(61)

Contracts programming for C++20 Contracts in C++

What is a contract?

A contract is the set ofpreconditions,postconditions andassertionsassociated to a function.

Precondition: What are theexpectationsof the function?

Postconditions: What must the functionensureupon termination?

Assertions: What predicates must be satisfied in specific locations of a function body?

It statesrightsandobligationsofclientandsupplier.

(62)

Contracts programming for C++20 Contracts in C++

What is a contract?

A contract is the set ofpreconditions,postconditions andassertionsassociated to a function.

Precondition: What are theexpectationsof the function?

Postconditions: What must the functionensureupon termination?

Assertions: What predicates must be satisfied in specific locations of a function body?

It statesrightsandobligationsofclientandsupplier.

(63)

Contracts programming for C++20 Contracts in C++

What is a contract?

A contract is the set ofpreconditions,postconditions andassertionsassociated to a function.

Precondition: What are theexpectationsof the function?

Postconditions: What must the functionensureupon termination?

Assertions: What predicates must be satisfied in specific locations of a function body?

It statesrightsandobligationsofclientandsupplier.

(64)

Contracts programming for C++20 Contracts in C++

What is a contract?

A contract is the set ofpreconditions,postconditions andassertionsassociated to a function.

Precondition: What are theexpectationsof the function?

Postconditions: What must the functionensureupon termination?

Assertions: What predicates must be satisfied in specific locations of a function body?

(65)

Contracts programming for C++20 Contracts in C++

Expectations

Precondition

A predicate that should hold upon entry into a function.

It expresses a function’s expectation on its arguments and/or the state of objects that may be used by the function.

Expressed by attributeexpects.

doublesqrt(doublex) [[expects:x>0]]; classqueue{

// ...

voidpush(constT&x) [[expects: !full() ]]; // ...

};

Preconditions use a modified attribute syntax. The expectation is part of the function declaration.

(66)

Contracts programming for C++20 Contracts in C++

Expectations

Precondition

A predicate that should hold upon entry into a function.

It expresses a function’s expectation on its arguments and/or the state of objects that may be used by the function.

Expressed by attributeexpects.

doublesqrt(doublex) [[expects:x>0]];

classqueue{ // ...

voidpush(constT&x) [[expects: !full() ]]; // ...

};

Preconditions use a modified attribute syntax. The expectation is part of the function declaration.

(67)

Contracts programming for C++20 Contracts in C++

Expectations

Precondition

A predicate that should hold upon entry into a function.

It expresses a function’s expectation on its arguments and/or the state of objects that may be used by the function.

Expressed by attributeexpects.

doublesqrt(doublex) [[expects:x>0]];

classqueue{ // ...

voidpush(constT&x) [[expects: !full() ]];

// ...

};

Preconditions use a modified attribute syntax. The expectation is part of the function declaration.

(68)

Contracts programming for C++20 Contracts in C++

Expectations

Precondition

A predicate that should hold upon entry into a function.

It expresses a function’s expectation on its arguments and/or the state of objects that may be used by the function.

Expressed by attributeexpects.

doublesqrt(doublex) [[expects:x>0]];

classqueue{ // ...

voidpush(constT&x) [[expects: !full() ]];

// ...

};

(69)

Contracts programming for C++20 Contracts in C++

Assurances

Postcondition

A predicate that should hold upon exit from a function.

It expresses the conditions that a function should ensure for the return value and/or the state of objects that may be used by the function.

Postconditions are expressed byensuresattributes.

doublesqrt(doublex) [[expects:x>=0]]

[[ensuresresult: result>=0]];

Postconditions may introduce a name for the result of the function.

(70)

Contracts programming for C++20 Contracts in C++

Assurances

Postcondition

A predicate that should hold upon exit from a function.

It expresses the conditions that a function should ensure for the return value and/or the state of objects that may be used by the function.

Postconditions are expressed byensuresattributes.

doublesqrt(doublex) [[expects:x>=0]]

[[ensuresresult: result>=0]];

Postconditions may introduce a name for the result of the function.

(71)

Contracts programming for C++20 Contracts in C++

Assurances

Postcondition

A predicate that should hold upon exit from a function.

It expresses the conditions that a function should ensure for the return value and/or the state of objects that may be used by the function.

Postconditions are expressed byensuresattributes.

doublesqrt(doublex) [[expects:x>=0]]

[[ensuresresult: result>=0]];

Postconditions may introduce a name for the result of the function.

(72)

Contracts programming for C++20 Contracts in C++

Assertions

Assertions

A predicate that should hold at its point in a function body.

It expresses the conditions that must be satisfied, on objects that are accessible at its point in a body.

Assertions are expressed by assert attributes.

doubleadd_distances(conststd::vector<double> &v) [[ensurres r: r>=0.0]]

{

doubler= 0.0; for (autox : v) {

[[assert: x >= 0.0]]; r +=x;

}

returnr; }

(73)

Contracts programming for C++20 Contracts in C++

Assertions

Assertions

A predicate that should hold at its point in a function body.

It expresses the conditions that must be satisfied, on objects that are accessible at its point in a body.

Assertions are expressed by assert attributes.

doubleadd_distances(conststd::vector<double> &v) [[ensurres r: r>=0.0]]

{

doubler= 0.0;

for (autox : v) { [[assert: x >= 0.0]];

r +=x;

}

returnr; }

(74)

Contracts programming for C++20 Contracts in C++

Effect of contracts

A contract has no observable effect on a correct program (except performance).

The only semantic effect of a contract happens if it is violated.

Why do we use attributes syntax? Contract may be checked or not. Attributes are not part of function type.

However,contracts are not an optional feature. As any other standardized attribute.

Contracts checking and corresponding effects depend on build system settings.

Default: Contract violationProgram termination.

(75)

Contracts programming for C++20 Contracts in C++

Effect of contracts

A contract has no observable effect on a correct program (except performance).

The only semantic effect of a contract happens if it is violated.

Why do we use attributes syntax?

Contract may be checked or not.

Attributes are not part of function type.

However,contracts are not an optional feature.

As any other standardized attribute.

Contracts checking and corresponding effects depend on build system settings.

Default: Contract violationProgram termination.

(76)

Contracts programming for C++20 Contracts in C++

Effect of contracts

A contract has no observable effect on a correct program (except performance).

The only semantic effect of a contract happens if it is violated.

Why do we use attributes syntax?

Contract may be checked or not.

Attributes are not part of function type.

However,contracts are not an optional feature.

As any other standardized attribute.

Contracts checking and corresponding effects depend on

(77)

Contracts programming for C++20 Contracts in C++

Repeating a contract

Any redeclaration of a function has either the same contract or completely omits the contract.

int f(int x) [[expects:x>0]] [[ensuresr:r >0]];

int f (int x) ; // OK. No contract. int f ( int x)

[[expects:x>=0]]; // Error missing ensures and different expects int f(int x)

[[expects:x>0]]

[[ensuresr:r >0]]; // OK. Same contract.

(78)

Contracts programming for C++20 Contracts in C++

Repeating a contract

Any redeclaration of a function has either the same contract or completely omits the contract.

int f(int x) [[expects:x>0]]

[[ensuresr:r >0]];

int f (int x) ; // OK. No contract.

int f ( int x)

[[expects:x>=0]]; // Error missing ensures and different expects

(79)

Contracts programming for C++20 Contracts in C++

Repeating a contract

But argument names may differ.

int f(int x) [[expects:x>0]] [[ensuresr:r >0]]; int f(int y)

[[expects:y>0]] [[ensuresz:z >0]];

(80)

Contracts programming for C++20 Contracts in C++

Repeating a contract

But argument names may differ.

int f(int x) [[expects:x>0]]

[[ensuresr:r >0]];

int f(int y) [[expects:y>0]]

[[ensuresz:z >0]];

(81)

Contracts programming for C++20 Contract checking

1 A brief history of contracts 2 Introduction

3 Contracts in C++

4 Contract checking 5 Contracts on interfaces 6 Final notes

(82)

Contracts programming for C++20 Contract checking

Assertion level

Every contract expression has an associatedassertion level.

Contract levels: default,audit,axiom.

Checks will be effectively performed depending on build mode.

Default level can be omitted.

void f(element&x) [[expects:x.valid() ]];

voidg(element&x) [[expects default:x.valid() ]]; Cost of checking is expected to be small compared to function execution.

(83)

Contracts programming for C++20 Contract checking

Assertion level

Every contract expression has an associatedassertion level.

Contract levels: default,audit,axiom.

Checks will be effectively performed depending on build mode.

Default level can be omitted.

void f(element&x) [[expects:x.valid() ]];

voidg(element&x) [[expects default:x.valid() ]]; Cost of checking is expected to be small compared to function execution.

(84)

Contracts programming for C++20 Contract checking

Assertion level

Every contract expression has an associatedassertion level.

Contract levels: default,audit,axiom.

Checks will be effectively performed depending on build mode.

Default level can be omitted.

void f(element&x) [[expects:x.valid() ]];

voidg(element&x) [[expects default:x.valid() ]];

Cost of checking is expected to be small compared to function execution.

(85)

Contracts programming for C++20 Contract checking

Assertion level

Every contract expression has an associatedassertion level.

Contract levels: default,audit,axiom.

Checks will be effectively performed depending on build mode.

Default level can be omitted.

void f(element&x) [[expects:x.valid() ]];

voidg(element&x) [[expects default:x.valid() ]];

Cost of checking is expected to be small compared to function execution.

(86)

Contracts programming for C++20 Contract checking

Audit checks

Anauditassertion levelis expected to be used in cases where the cost of a run-time check is assumed to be large compared to function execution.

Or at least significant.

template<typenameIt,typenameT>

boolbinary_search(It first , It last, constT&x) [[expects audit:is_sorted(first,last) ]];

(87)

Contracts programming for C++20 Contract checking

Axiom checks

Anaxiomassertion levelis expected to be used in cases where the run-time check willneverbe performed.

Still they need to be valid C++.

They are formal comments for humans and/or static analyzers.

template<typenameInputIterator>

InputIterator my_algorithm(InputIterator first , InputIterator last) [[expects axiom:first!=last &&reachable(first,last) ]];

Axioms are not evaluated.

They may contain calls to declared but undefined functions.

(88)

Contracts programming for C++20 Contract checking

Axiom checks

Anaxiomassertion levelis expected to be used in cases where the run-time check willneverbe performed.

Still they need to be valid C++.

They are formal comments for humans and/or static analyzers.

template<typenameInputIterator>

InputIterator my_algorithm(InputIterator first , InputIterator last) [[expects axiom:first!=last &&reachable(first,last) ]];

Axioms are not evaluated.

They may contain calls to declared but undefined functions.

(89)

Contracts programming for C++20 Contract checking

Axiom checks

Anaxiomassertion levelis expected to be used in cases where the run-time check willneverbe performed.

Still they need to be valid C++.

They are formal comments for humans and/or static analyzers.

template<typenameInputIterator>

InputIterator my_algorithm(InputIterator first , InputIterator last) [[expects axiom:first!=last &&reachable(first,last) ]];

Axioms are not evaluated.

They may contain calls to declared but undefined functions.

(90)

Contracts programming for C++20 Contract checking

Build levels

Every translation is performed in abuild level:

off: No run-time checking is performed.

default: Checks withdefaultlevels are checked.

audit: Checks withdefaultandauditlevels are checked.

How do you select thebuild level: No way of selecting in source code. An option from your compiler.

(91)

Contracts programming for C++20 Contract checking

Build levels

Every translation is performed in abuild level:

off: No run-time checking is performed.

default: Checks withdefaultlevels are checked.

audit: Checks withdefaultandauditlevels are checked.

How do you select thebuild level:

No way of selecting in source code.

An option from your compiler.

(92)

Contracts programming for C++20 Contract checking

Contract checking

If a function has multiple preconditions or postconditions that would be checked, theirevaluation will be performed in the order they appear

voidf(int p)

[[expects:p!=nullptr]]

[[expects:∗p== 0]] // Only checked when p!=nullptr {

∗p= 1; }

(93)

Contracts programming for C++20 Contract checking

Contract checking

If a function has multiple preconditions or postconditions that would be checked, theirevaluation will be performed in the order they appear

voidf(int p)

[[expects:p!=nullptr]]

[[expects:∗p== 0]] // Only checked when p!=nullptr {

∗p= 1;

}

(94)

Contracts programming for C++20 Contract checking

Contract violation handlers

A translation unit has an associated contract violation handler.

A contract violation handler is the function to be called when a contract is broken.

Function with specific signature. void(conststd::contract_violation &);

If you do not supply a handler, the default isstd::abort(). If you want to supply a handler:

No way of setting through source code. No way of asking which is current handler. An option in your compiler to supply it.

Security sensitive systems may prevent arbitrary handlers.

Referenzen

ÄHNLICHE DOKUMENTE

When read from this point of view, Tennyson’s version of the story of Tiresias becomes a magnificent event thanks to the profusion of light coming from the

The authors concluded in Hofer-Szabo´, Re´dei, and Szabo´ ([1999]) that the apparent contradiction between this possibility and the claim that the EPRB correlations do not allow for

Coronary revascularization, either by CABG or PTCA, has been proven unequivocally to improve survival in selected patients, especially in those with triple vessel disease and

In summary, the absence of lipomatous, sclerosing or ®brous features in this lesion is inconsistent with a diagnosis of lipo- sclerosing myxo®brous tumour as described by Ragsdale

(Specifying the minimum support may be a serious obstacle since the.. ,,appropriate” minimum support is difficult to determine. By contrast to, for example, the significance level

A further crucial dimension not yet seriously addressed at Machakos is how to bring into the process the views of other Sudanese groups deeply disaffected with the

For many Muslims, the self-declared Caliph of ISIS/Daesh is in a similar position – he has no legitimacy to make these claims, is not behaving as a Caliph should, and is

Following the model of the mechanism for primary psychological support to employees – peer support persons in place in the Ministry of Interior – the NAP 1325 provided for the