6.6. Constant Expressions and Default Parameters

Template parameters are not limited only to types, although until now I’ve limited my discussion to them. We can also declare constant expressions as parameters. For example, our numeric sequence class hierarchy might be redefined as a template class in which the number of elements an object contains is parameterized:

template <int len> 
class num_sequence { 
public: 
    num_sequence( int beg_pos=1 ); 
    // ... 
}; 

template <int len> 
class Fibonacci : public NumericSeries<len> { 
public: 
    Fibonacci( int beg_pos=1 ) 
         : num_sequence<len>( beg_pos ){} 
    // ... 
}; 

When a Fibonacci object is created, as in

Fibonacci< 16 > fib1; 
Fibonacci< 16 > fib2( 17 ); 

instances of both the Fibonacci derived class and the ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.