go to start Ex W2
|home |print view |recent changes |changed March 2, 2018 |
|You are <- set your identity!

Sections: Exercises Week 02 | =[dynArray]= Extension | =[pop_back]= with optional<T> | Class Template Deduction | Directory-Sizer | Fold Expressions | =[if constexpr]= (Preview Exercise, Optional) |

Exercises Week 02 ^

dynArray Extension ^

You have either already implemented the dynArray known from the C++ module previously, or you can take the solution of that exercise as a starting point for the tasks of the following exercises [1]. You will add some extensions to the dynArray container.

[1] https://github.com/PeterSommerlad/CPlusPlusLecture/tree/master/solws12/dynArray

Important: If you import the project, you need to adjust the compiler flag settings: Set the dialect to -std=c++1z! (Project Properties -> C++ Build -> Settings -> GCC C++ Compiler -> Dialect. Use the input field to enter the dialect, as C++17 is not available in the dropdown menu.

pop_back with optional<T> ^

The pop_back call, which gets forwarded to vector::pop_back features undefined behavior in case of an empty container. And due to separation of action and query functions does not return the popped value. Change the implementation of the dynArray::pop_back member function to return a copy of the popped value wrapped in an std::optional<T>. If the container is empty, the std::optional<T> shall be empty too.

Hint: Write test cases first

Class Template Deduction ^

Your test cases (probably), and the test cases of the solution of dynArray don't benefit from the template argument deduction for class templates yet. Go though the test cases and figure out in which declarations the explicit specification of the template arguments can already be omittet.

Experiment: When the dynArray is initialized with string literals ("Hello") the deduced type will be char const *. However, deducing the type std::string might be preferable. If you are very eager you can try to facilitate class templates and specializations thereof to replace char const * with std::string for this case. We will cover this topic later in the semester though.

Directory-Sizer ^

Write a program using the filesystem library to compute the total size of all files in a given directory recursively. If no commmand line arguments are given take the current directory. If multiple command-line arguments are given sum up each individually and also present the total. Check if the information you can obtain from your OS corresponds with the output from your program.

call it like:

   $ mydirsize


   $ mydirsize /tmp


   $ mydirsize dirA dirB

How can you write test cases for your functionality? Note: you can employ the filesystem library also in your test cases!


Fold Expressions ^

You already implemented the readln template function, which takes an std::istream and a variadic number of further arguments (by reference). It reads from the stream "into" each argument. The implementation required the specification of a variadic template and a base case (or a sizeof... check), for a call without arguments. The implementation could have looked as follows:

void doread(std::istream &) {

template<typename ...I>
void doread(std::istream &in, I&...vars);

template<typename FIRST, typename ...I>
void doread(std::istream &in, FIRST &var, I&...vars) {
    in >> var;

if constexpr (Preview Exercise, Optional) ^

One new feature in C++17 we have not discussed in the lecture yet, are if constexpr statements. Using if constexpr provides the possibility to tell the compiler to conditionally enable code (at compile time), depending on some expression. This can be useful if you either have a system with distinct configurations for a target device, if your code depends on different versions of a library with more or less capabilities or to enable/disable certain sections in template code. It has already been possible to deal with such cases using the preprocessor or advanced template programming, but if constexpr features a much more readable way of achieving such things.

Let's have a look a a possible solution for the increment function from the last exercises. We provide the code on _Compiler Explorer_: https://godbolt.org/g/JG5FKX

|home |print view |recent changes |changed March 2, 2018 |
|You are <- set your identity!

Ex W2
go to start