我如何在没有增强的情况下将C ++ 11代码转换为C ++ 98? [英] How can I convert the C++11 code to C++98 without boost?

查看:38
本文介绍了我如何在没有增强的情况下将C ++ 11代码转换为C ++ 98?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能:

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);
    }
  }
}

我如何使它与C ++ 98一起使用?我试过了:

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

我该怎么做?

推荐答案

假设所使用的容器遵循正常的 std 约定,则可以明确地说明类型:

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;
    }
  }
}

这篇关于我如何在没有增强的情况下将C ++ 11代码转换为C ++ 98?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆