// Const_value.C
// ----------------------------------------------------------------
// COMP 290-001: Algorithm Library Design, Lutz Kettner, 01/11/2000
// Example of an input iterator with a constant value.

#include <assert.h>

template <class T>
class Const_value {
    T t;
public:
    // Default Constructible !
    Const_value() {}  
    Const_value( const T& s) : t(s) {}

    // Assignable by default.

    // Equality Comparable (not so easy what that should mean here)
    bool operator==( const Const_value<T>& cv) const { return ( this == &cv); }
    bool operator!=( const Const_value<T>& cv) const { return !(*this == cv); }

    // Trivial Iterator:
    const T& operator* () const { return  t; }
    const T* operator->() const { return & operator*(); }

    // Input Iterator
    Const_value<T>& operator++() { return *this; }
    Const_value<T>  operator++(int) {
        Const_value<T> tmp = *this;
        ++*this;
        return tmp;
    }
};

template <class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n( InputIterator first, Size n, OutputIterator result) {
    while ( n-- != 0)
	*result++ = *first++;
    return result;
}

int main() {
    int a[100];
    Const_value<int> cv( 42);
    copy_n( cv, 100, a);  // fills a with 100 times 42.
    assert( a[99] == 42);
}

