 Ex W13 |home |print view |recent changes |changed May 24, 2018 | exact |You are 3.237.186.116 <- set your identity!

## Peter's Simple Strong Typing^

Take the file psst.h write test cases for the strong value types, like Width, Height, and Radius from the lecture.

```struct Width:Psst::strong<int,Width>,Psst::ops<Width,Psst::Out>{};
struct Height:Psst::strong<int,Height>,Psst::ops<Height,Psst::Out>{};
```

• Write own strong types for Kilometer and Miles.
• Write tests against your strong types
• Extend Psst with other useful operations, such as `Sub` or `MultiplyWith` that can be mixed into Kilometer and Miles operations. Write tests for them
• Can you provide a generic conversion ConvertTo<Target,Ratio>::apply mix in ops type to convert between Kilometers and Miles, so that they can be combined?
• Can you provide a mixed Addition employing that conversion?

## constexpr Operator for Arithmetic and Comparisons of `std::ratio`^

There is a type in the standard library for representing ratios (`std::ratio`): http://en.cppreference.com/w/cpp/numeric/ratio/ratio

Implement the normal arithmetic operators for values of `std::ratio<N, D>` types and return the correct resulting type using `std::ratio_add<>` etc.

• `operator +`
• `operator -`
• `operator *`
• `operator /`

Do the same for the comparison operators `std::ratio_less<R1, R2>` etc.

• `operator <`
• `operator >`
• `operator ==`
• `operator !=`
• `operator <=`
• `operator >=`

### Extensions^

• Make sure the overloaded operator template functions are only considered for arguments of types instantiated from `std::ratio`.

• Define a variable template `ratiov` that allows you simpler ratio value generation than `std::ratio<1,2>{}`.

• Provide also UDL suffixes `_num` and `_den` in the namespace `ratio_literals`, so that `static_assert(2_num * 3_den == std::ratio<2, 3>{}, "ratio literals OK");` compiles, when you are `using namespace ratio_literals;`

### Hint^

You can use the following definition of `is_ratio`:

```template <typename R>
constexpr bool is_ratio{false};
template <intmax_t N, intmax_t D>
constexpr bool is_ratio<std::ratio<N,D>>{true};
```

## McCarthy 91 Compile Time Function^

The McCarthy 91 function is a recursive function for formal verification (https://en.wikipedia.org/wiki/McCarthy_91_function). We will just implement it for exercising compile-time constructs in C++.

Implement the following versions of the function (of course write tests first):

• Run-time version of the function: `mc_carthy(15)`
• Make it a compile-time version using constexpr: `mc_carthy(15)`
• Implement it with C++03 features (a class template with a static member variable containing the value and a specialization for one case): `mc_carthy_t<15>::value`
• Implement it as a variable template: `mc_carthy_v<15>`
• First, use `mc_carthy_t` for calculating the value
• Second, implement the variable template without using the `mc_carthy_t` template
• Try to implement the McCarthy91 function using function templates: mc_carthy<15>(). This requires SFINAE.
• Provide a literal operator for the `_mc91` suffix: `15_mc91`

 |home |print view |recent changes |changed May 24, 2018 | exact |You are 3.237.186.116 <- set your identity! Ex W13 