J
Jon Wilson
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?
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?