Mentions légales du service

Skip to content
Snippets Groups Projects
Commit cb7cf9c4 authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Refactoring in operators TP.

parent 37e9dac6
No related branches found
No related tags found
No related merge requests found
Showing with 2814 additions and 515 deletions
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb)
%% Cell type:markdown id: tags:
* [Introduction to the concept of operator overload](/notebooks/3-Operators/1-Intro.ipynb)
* [TP 8](/notebooks/3-Operators/1b-TP.ipynb)
* [TP 9](/notebooks/3-Operators/1b-TP.ipynb)
* [Comparison operators](/notebooks/3-Operators/2-Comparison.ipynb)
* [Stream operators](/notebooks/3-Operators/3-Stream.ipynb)
* [TP 9](/notebooks/3-Operators/3b-TP.ipynb)
* [TP 10](/notebooks/3-Operators/3b-TP.ipynb)
* [Affectation operator and the canonical form of a class](/notebooks/3-Operators/4-CanonicalForm.ipynb)
* [Functors](/notebooks/3-Operators/5-Functors.ipynb)
* [TP 10](/notebooks/3-Operators/5b-TP.ipynb)
* [TP 11](/notebooks/3-Operators/5b-TP.ipynb)
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 8](/notebooks/3-Operators/1b-TP.ipynb)
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 9](/notebooks/3-Operators/1b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#EXERCICE-31:-replace-PowerOfTwoApprox::DoubleValue-by-an-operator-double" data-toc-modified-id="EXERCICE-31:-replace-PowerOfTwoApprox::DoubleValue-by-an-operator-double-2">EXERCICE 31: replace <code>PowerOfTwoApprox::DoubleValue</code> by an <code>operator double</code></a></span></li><li><span><a href="#EXERCICE-32:-replace-PowerOfTwoApprox::Multiply-by-operator-*" data-toc-modified-id="EXERCICE-32:-replace-PowerOfTwoApprox::Multiply-by-operator-*-3">EXERCICE 32: replace <code>PowerOfTwoApprox::Multiply</code> by <code>operator *</code></a></span></li><li><span><a href="#EXERCICE-33:-commutativity" data-toc-modified-id="EXERCICE-33:-commutativity-4">EXERCICE 33: commutativity</a></span></li><li><span><a href="#EXERCICE-34:-operator[]" data-toc-modified-id="EXERCICE-34:-operator[]-5">EXERCICE 34: <code>operator[]</code></a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#EXERCICE-26:-replace-PowerOfTwoApprox::AsDouble-by-an-operator-double" data-toc-modified-id="EXERCICE-26:-replace-PowerOfTwoApprox::AsDouble-by-an-operator-double-2">EXERCICE 26: replace <code>PowerOfTwoApprox::AsDouble</code> by an <code>operator double</code></a></span></li><li><span><a href="#EXERCICE-27:-replace-PowerOfTwoApprox::Multiply-by-operator-*" data-toc-modified-id="EXERCICE-27:-replace-PowerOfTwoApprox::Multiply-by-operator-*-3">EXERCICE 27: replace <code>PowerOfTwoApprox::Multiply</code> by <code>operator *</code></a></span></li><li><span><a href="#EXERCICE-28:-operator[]" data-toc-modified-id="EXERCICE-28:-operator[]-4">EXERCICE 28: <code>operator[]</code></a></span></li></ul></div>
%% Cell type:markdown id: tags:
### Introduction
[This notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP.
%% Cell type:markdown id: tags:
### EXERCICE 31: replace `PowerOfTwoApprox::DoubleValue` by an `operator double`
### EXERCICE 26: replace `PowerOfTwoApprox::AsDouble` by an `operator double`
The arguments should remain the same.
%% Cell type:markdown id: tags:
### EXERCICE 32: replace `PowerOfTwoApprox::Multiply` by `operator *`
### EXERCICE 27: replace `PowerOfTwoApprox::Multiply` by `operator *`
Arguments remain unchanged.
%% Cell type:markdown id: tags:
### EXERCICE 33: commutativity
In `TestDisplaySum::Display()`, you should have the line
Please ensure the operator is commutative: we want the line
````
int approx = approx1 * coefficient1 + approx2 * coefficient2;
int approx = approximation1 * coefficient1 + coefficient2 * approximation2;
````
Try the following one instead:
````
int approx = coefficient1 * approx1 + coefficient2 * approx2;
````
You should get at least a warning doing so... (at least with the CMake build I provide that activates `-Wconversion` warning - more on that [later](/notebooks/6-InRealEnvironment/3-Compilers.ipynb)...)
Add a print line in `operator*`.
Do you understand what happens?
[Solution](/notebooks/3-Operators/1c-SolutionExercice33.ipynb)
to work.
%% Cell type:markdown id: tags:
### EXERCICE 34: `operator[]`
### EXERCICE 28: `operator[]`
In `TestDisplayContainer`, replace `GetElement()` method by `operator[]`.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 8 - Solution](/notebooks/3-Operators/1b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-33:-commutativity" data-toc-modified-id="EXERCICE-33:-commutativity-1">EXERCICE 33: commutativity</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 33: commutativity
What happens in fact is that `operator*` is not commutative: it expects as first argument an object and as second argument an integer.
So why does it work nonetheless?
`operator*(int, PowerOfTwoApprox)` is not defined... but addition between an integer and a double is obviously possible, AND you've just defined before an implicit conversion to `double`.
You should of course heed the warning and fix your code:
* Make `operator double` explicit. Except in very specific cases, I advise you to always make such conversion operators explicit to avoid unintended side effects as the one we've just got.
* If commutativity is important for you, make `operator*` a free function instead of a method and provide both ordering of arguments.
The solution file implements both these modifications.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 9](/notebooks/3-Operators/3b-TP.ipynb)
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 10](/notebooks/3-Operators/3b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-35:-operator<<" data-toc-modified-id="EXERCICE-35:-operator<<-1">EXERCICE 35: <code>operator&lt;&lt;</code></a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-29:-operator<<" data-toc-modified-id="EXERCICE-29:-operator<<-1">EXERCICE 29: <code>operator&lt;&lt;</code></a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 35: `operator<<`
### EXERCICE 29: `operator<<`
Introduce an `operator<<` for class `PowerOfTwoApprox` which provides the numerator and exponent used, e.g. `10/2^4`.
Use it in `TestDisplayPowerOfTwoApprox::Display()`.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 10](/notebooks/3-Operators/5b-TP.ipynb)
# [Getting started in C++](/) - [Operators](/notebooks/3-Operators/0-main.ipynb) - [TP 11](/notebooks/3-Operators/5b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-36:-functor" data-toc-modified-id="EXERCICE-36:-functor-1">EXERCICE 36: functor</a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-30:-functor" data-toc-modified-id="EXERCICE-30:-functor-1">EXERCICE 30: functor</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 36: functor
### EXERCICE 30: functor
Replace method `Do()` in `TestDisplay` and its derived classes by a functor.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
......@@ -395,9 +395,6 @@ private:
};
TestDisplayContainer::TestDisplayContainer(std::size_t capacity)
: capacity_(capacity)
{
......@@ -456,7 +453,7 @@ void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDispla
{
for (auto i = 0ul; i < container.GetSize(); ++i)
{
auto& current_test_display = container.GetElement(i);
decltype(auto) current_test_display = container.GetElement(i);
for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit)
current_test_display.Do(Nbits);
......
cmake_minimum_required(VERSION 3.9)
# Add a compiler flag only if it is accepted.
# This macro is usually defined once and for all in a specific file which is included in the CMakeLists.txt in which
# you need it.
include(CheckCXXCompilerFlag)
macro(add_cxx_compiler_flag _flag)
string(REPLACE "-" "_" _flag_var ${_flag})
check_cxx_compiler_flag("${_flag}" CXX_COMPILER_${_flag_var}_OK)
if(CXX_COMPILER_${_flag_var}_OK)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}")
endif()
endmacro()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()
set(CMAKE_CXX_COMPILER clang++ CACHE STRING "C++ compiler")
set(CMAKE_C_COMPILER clang CACHE STRING "C compiler")
set(CMAKE_CXX_STANDARD 17 CACHE INTEGER "Version of the C++ standard to use")
set(CMAKE_CXX_EXTENSIONS OFF CACHE BOOL "If ON the GNU version of the standard is used.")
include(../../cmake/Settings.cmake)
project(GettingStartedWithModernCpp_TP_Operators)
add_cxx_compiler_flag("-Weverything") # all warnings for clang
add_cxx_compiler_flag("-Wall") # for gcc (and recognized by clang)
add_cxx_compiler_flag("-Wextra") # for gcc (and recognized by clang)
add_cxx_compiler_flag("-Wconversion")# for gcc
add_cxx_compiler_flag("-Wno-c++98-compat") # We assume we are using modern C++
add_cxx_compiler_flag("-Wno-c++98-compat-pedantic") # We assume we are using modern C++
add_cxx_compiler_flag("-Wno-padded")
include(../../cmake/AfterProjectSettings.cmake)
add_executable(initial initial_file.cpp)
# add_executable(exercice31 exercice31.cpp)
# add_executable(exercice32 exercice32.cpp)
# add_executable(exercice33 exercice33.cpp)
# add_executable(exercice34 exercice34.cpp)
# add_executable(exercice35 exercice35.cpp)
# add_executable(exercice36 exercice36.cpp)
\ No newline at end of file
# add_executable(exercice26 exercice26.cpp)
# add_executable(exercice27 exercice27.cpp)
# add_executable(exercice28 exercice28.cpp)
# add_executable(exercice29 exercice29.cpp)
# add_executable(exercice30 exercice30.cpp)
This diff is collapsed.
../2-ObjectProgramming/Solution/exercice30.cpp
\ No newline at end of file
../2-ObjectProgramming/Solution/exercice25.cpp
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment