B
Bartholomew Simpson
I'm trying to avoid (or at least, minimize) duplicity of effort. I have
the following function:
void Permissions::Exists(const unsigned int id,
std::vector<Permission>::const_iterator& iter)
{
if (m_permissions.empty())
iter = m_permissions.end() ;
std::vector<Permission>::const_iterator cit=m_permissions.begin();
for (cit; cit != m_permissions.end(); cit++)
{
if (cit->Id() == id)
break ;
}
iter = cit ;
}
It can take the ff combinations of args:
string, iter
string, const_iter
unsigned int, iter
unsigned int, const_iter
The logic is essentially the same for all cases - I would like to write
this as a template function, like:
template < class T1, class T2)
void Permissions::Exists(T1 key, T2 iterator_type)
I have the ff questions:
1). I don't ever recall seeing an iterator being passed as template
parameter (although I can't see why not - since it has a type) - is it
legal to use an iterator as a 'type' in a template func?
2). It appears like I may have to use template specialization (because I
am looking at different fields when doing the 'key comparison' - in
which case, my idea to use templates as "shorthand" (or as a compile
time code generator), is invalidated because I will still have to write
4 specializations ... am I thinking along the right lines, or am I
missing something (i.e. can I implement the above function as ONE
template function instead of four)?
3). last but not the least - a general template programming question
relating to 'const correctness' - is passing a const variable to a
template func treated as a different type from a non-const - i.e. if I
have the ff:
template <class T>
T foo(T var);
If I pass a const int variable to the function, and then in another part
of the code, pass a non-const int to the foo function - will the
compiler generate code for the two "functions"
const int foo(const int);
int foo(int);
(probably an ill posed question - but I hope you understand what I'm
getting at)
the following function:
void Permissions::Exists(const unsigned int id,
std::vector<Permission>::const_iterator& iter)
{
if (m_permissions.empty())
iter = m_permissions.end() ;
std::vector<Permission>::const_iterator cit=m_permissions.begin();
for (cit; cit != m_permissions.end(); cit++)
{
if (cit->Id() == id)
break ;
}
iter = cit ;
}
It can take the ff combinations of args:
string, iter
string, const_iter
unsigned int, iter
unsigned int, const_iter
The logic is essentially the same for all cases - I would like to write
this as a template function, like:
template < class T1, class T2)
void Permissions::Exists(T1 key, T2 iterator_type)
I have the ff questions:
1). I don't ever recall seeing an iterator being passed as template
parameter (although I can't see why not - since it has a type) - is it
legal to use an iterator as a 'type' in a template func?
2). It appears like I may have to use template specialization (because I
am looking at different fields when doing the 'key comparison' - in
which case, my idea to use templates as "shorthand" (or as a compile
time code generator), is invalidated because I will still have to write
4 specializations ... am I thinking along the right lines, or am I
missing something (i.e. can I implement the above function as ONE
template function instead of four)?
3). last but not the least - a general template programming question
relating to 'const correctness' - is passing a const variable to a
template func treated as a different type from a non-const - i.e. if I
have the ff:
template <class T>
T foo(T var);
If I pass a const int variable to the function, and then in another part
of the code, pass a non-const int to the foo function - will the
compiler generate code for the two "functions"
const int foo(const int);
int foo(int);
(probably an ill posed question - but I hope you understand what I'm
getting at)