Skip to content

Page 460: In exercise 13-5, the TODO comment for fib_sum refers to the wrong thing. #107

@WraithGlade

Description

@WraithGlade

The following example code in exercise 13-5 refers to the wrong thing:

long fib_sum(size_t n) { 
    // TODO: Adapt code from Exercise 12.1
    return 0; 
}

Here is the actual contents of Exercise 12.1:

12-1. Reimplement the narrow_cast in Listing 6-6 to return a std::optional. If the cast would result in a narrowing conversion, return an empty optional rather than throwing an exception. Write a unit test that ensures your solution works.

Perhaps you instead intended to refer to the recursive version of the Fibonacci algorithm, considering that its naive implementation performance is terrible and easily noticed. Or perhaps you are only intending for the summing of the Fibonacci sequence itself to be the source of the performance overhead. The clarity here could be improved.

...

Also, why are you passing by const reference in the following example code, from that same example code block?:

long cached_fib_sum(const size_t& n) { 
    static std:: map <long, long> cache; 
    //TODO: Implement me 
    return 0; 
}

There doesn't seem to be a point to doing that, although it doesn't really harm anything either in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions