How to convert C ++ 11 code to C ++ 98 without raising?

I have a 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 this does not work and gives me an error, for example:

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

and etc.

How can i do this?

+4
source share
4 answers

Assuming the containers you use comply with normal conventions std, you can explicitly specify the types:

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;
    }
  }
}
+5
source

You can always replace autowith templates because they follow the same type of output 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 incElementto accept its argument by reference. Otherwise, the copy of the container will be changed and the client will not be able to observe the changes.

+6

The first version of your function is containerTnot a template. This is a class (and it may be a template instance, but it does not matter).

If containerTsatisfies the standard container concept, you should write:

for (typename containerT::iterator i ...) {
    for (typename containerT::value_type::iterator j ...)
+4
source

Well, the compiler tells you where the problem is. You declare containerTas a type, but use it as a template. Therefore, you can try changing containerTto the template template argument (the same for containerRow).

template<template<class> class containerT, template<class> class containerRowT, typename containerElementT>
void incElement(containerT<containerRowT<containerElementT> > c);

What about this for STL containers:

template<typename containerT>
void incElement(containerT c){
  for(typename containerT::iterator i = c.begin(); i != c.end(); ++i) {
    for(typename containerT::value_type::iterator j = (*i).begin(); j != (*i).end(); ++j) {
       ++(*j);
    }
  }
}
0
source

Source: https://habr.com/ru/post/1529166/


All Articles