Ex W1

Set up your environment

If you don't already have a development environment with Cevelop and a recent compiler (at least GCC 6.1.0) take care of this first. Here is a guide from CPl on how to do it: https://wiki.ifs.hsr.ch/CPlusPlus/ExW1

We will most likely need newer compilers later on in the module. Details about the specifc version will be supplied then.

Ctylechecker & Constificator (when using Cevelop)

If you don't have already done that install the Ctylechecker plug-in into your Cevelop IDE.

Import one of your exercise solutions from the C++ module and check it for problems reported by Ctylechecker or Constificator. Are the reports reasonable?

If you find any errors in the plug-ins let me know. We might be able to fix them!

Easy C++ Repetition Tasks

For the following tasks write test cases first!

  1. Create a function that takes an int as parameter and returns the next value (increment).
  2. Add an overload of increment that modifies the parameter as side-effect. Do you encounter any problems when calling increment in the presence of both overloads? Stick with the side-effect version for the following exercises.
  3. For signed integers overflow is undefined behavior in C++. Make your function safe. If such an overflow would happen throw an exception instead of incrementing the value (std::overflow_error).
  4. Change your increment to accept parameters of arbitrary type and increments them - given they support the prefix increment operator. Start your test cases with double.
  5. Overflow for unsigned is well defined. Add an overload for this case that does not throw. Does it need to be a template specialization?
  6. Several overloads are required to cover all cases of unsigned type (i.e. unsigned, unsigned long) because the template has an exact match for the overload resolution unless you provide an overload for the specific type. Example: If you provide the overload for unsigned long long and call increment with an unsigned variable as argument the template will be chosen instead of the unsigned long long overload. Therefore, try an alternative approach to tackle overflows. Create a predicate (as template) allow_overload that returns false by default but can and should be specialized for unsigned types to be true. You can implement the predicate as variable, class or function template for that matter.
  7. With all the template specializations for the different unsigned types there is still much code, at least it avoids duplication in the logic. As usually the standard library can help. Have a look at std::numeric_limits::is_integer and the is_unsigned type trait. Can you implement your predicate with those?
  8. Can you also pass an iterator to your increment function? You might experience different outcome depending on your compiler. It might not be happy if you want to get numeric_limits for your iterator. Adapt your increment function to also have the capability to take a second parameter to have a user-provided limit. Your previous tests should still remain working. Therefore, add a default argument to that parameter. You probably used comparisons and a - 1 operation to your limit. Does this restrict the iterators you can use with increment.

Fraction Type

Implement your own Fraction type that can represent fraction numbers more precisely than the double type.

Last edited February 23, 2017