template member functions of a templated class

From: Jon Wilson (jsw_at_fnal.gov)
Date: 01/07/05


Date: Fri, 07 Jan 2005 13:55:07 -0600

I have a class which needs to accumulate data. The way we get this data
is by calling a member function which returns float on a number of
different objects of different type (they are all the same type for a
given instance of the class, but different types for different instances.)

#include<set>

using namespace std;

template<class T>
class Data
{
        private:
                set<float> m_data;
                mem_fun_ref_t<float, T> m_ftn;
        public:
                Data() :m_data() {}
}

This is good. Now, I want a function which takes a range of iterators
(which dereference to type T, or in some cases to type T*), and adds all
the data from the iterator range to m_data. It seems like I should be
able to do this with one, or at most two, templated member functions.
How do I deal with having templated member functions inside the
templated class?

template<class T>
class Data
{
---snip
        public:
                template<class Container>
                Add_Data(typename Container<T>::iterator begin,
                         typename Container<T>::iterator end);
};

gives me the error: Non-template type Container used as a template.

So I try

--snip
        public:
                template<class C<T> >
                Add_Data(typename C<T>::iterator begin,
                         .... end);
--snip

Which gets the error: C is not a template.

I looked up the STL vector constructor which takes a range of iterators,
and it simply uses a construction like:

--snip
        public:
                template<class _InputIter>
                Add_Data(typename _InputIter begin,
                         ... end);
--snip

However, it seems like then, you could get types which were not in fact
iterators, and even if they were iterators, would dereference to a type
other than T.

Is there a way to restrict what types are acceptable for the template so
that I only get iterators which dereference to T (or T*)?

Also, given that some of the containers I will be taking data from store
T's and some store T*'s, do I need two template functions for these two
possibilities? or just one? or some type of template specialization?



Relevant Pages

  • Re: template member functions of a templated class
    ... I want a function which takes a range of iterators ... > able to do this with one, or at most two, templated member functions. ... C is not a template. ... See a good book on a better explanation of what InputIterator ...
    (comp.lang.cpp)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to make obj of temp in temp ???
    ... Then you need to fix the typo in the variable initialization ... template< class T> ... member functions if they existed, unless of course if they are static ... But maybe I just cannot see a good reason, does anyone else see a benefit to ...
    (microsoft.public.vc.language)
  • Re: Interface of the set classes
    ... > container usually depend on the data set. ... > templates, iterators, probably tags, and on. ... Python has iterators, and doesn't need templates, since signature based ... The equivalent of "specializing a template" ...
    (comp.lang.python)
  • How can I use a non-trivial constructor with as little code duplication as possible?
    ... template <typename T> ... /* As with member functions of class templates, ... and I would like to use the base class Singleton as much as ...
    (comp.lang.cpp)