I have the function:
template<typename containerT>
void incElement(containerT c){
for(auto i = c.begin(); i != c.end(); ++i) {
for(auto j = (*i).begin(); j != (*i).end(); ++j) {
++(*j);
}
}
}
How can I make this work with C++98? I tried:
template<typename containerT, typename containerRowT, typename containerElementT>
void incElement(containerT<containerRowT<containerElementT> > c) {
for(containerT<containerRowT<containerElementT> >::iterator i = c.begin(); i != c.end; ++i) {
for(containerRowT<containerElementT> >::iterator j = (*i).begin(); j != (*j).end(); ++j){
++(*j);
}
}
}
And it does not work and gives me error like:
test.cpp:4:17: error: ‘containerT’ is not a template
void incElement(containerT<containerRowT<containerElementT> > c) {
^
test.cpp:4:28: error: ‘containerRowT’ is not a template
void incElement(containerT<containerRowT<containerElementT> > c) {
^
test.cpp: In function ‘void incElement(containerT)’:
test.cpp:5:7: error: ‘containerT’ is not a template
etc.
How can I do this?
You can always replace auto
by templates, because they follow the same type deduction rules:
template<typename Iterator>
void inner(Iterator begin, Iterator end)
{
for (; begin != end; ++begin)
{
++*begin;
}
}
template<typename Iterator>
void outer(Iterator begin, Iterator end)
{
for (; begin != end; ++begin)
{
inner(begin->begin(), begin->end());
}
}
template<typename Container>
void incElement(Container& container)
{
outer(container.begin(), container.end());
}
Note that I changed the signature of incElement
to accept its argument by reference. Otherwise, a copy of the container would be modified, and the client would not be able to observe any changes.
Assuming the containers used follow normal std
conventions, you can spell out the types explicitly:
template <typename containerT>
void incElement(containerT &c) //assuming you want a reference here, otherwise you'll be mnodifying a local copy only
{
typedef typename containerT::iterator TypeOfI;
typedef typename containerT::value_type TypeOfStarI;
typedef typename TypeOfStarI::iterator TypeOfJ;
for (TypeOfI i = c.begin(); i != c.end(); ++i) {
for (TypeOfJ j = i->begin(); j != i->end(); ++j) {
++*j;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With