EPI1.5.3: Problem 13.4 LRU ISBN cache

#1

This could either be a dumb question or its a bug in the code.
In the solution, we use a class LRUcache templated with <size_t capacity>

template <size_t capacity>
class LRUCache
{
    // blah blah blah

};

We use this capacity later in a function.
But this capacity is never stored as part of a member variable or anywhere else.
I was expecting a parameterized constructor which would store this capacity as a member variable so that it could be used in functions.
How can we access this, assuming here only default constuctor of LRUCache gets invoked?
Is there something special about size_t?

0 Likes

#2

it’s not a bug; it’s just a non-type template parameter.

likewise, the following works if you test it.

template < int N >
class Foo
{
public:
    void printN()
    {
        std::cout << N;
    }
};

N’s value is determined at compile time, and i believe when compiler compiles this template, N is directly replaced with whatever value you used for that parameter.

1 Like