如何在不提升的情况下将 C++11 代码转换为 C++98
How can I convert the C++11 code to C++98 without boost?
>我有以下函数:
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 完成此操作?我试过了:
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);
}
}
}
它不起作用并给我错误,例如:
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
等。
我该怎么做?
您始终可以将auto
替换为模板,因为它们遵循相同的类型扣除规则:
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());
}
请注意,我更改了incElement
的签名以通过引用接受其参数。否则,将修改容器的副本,并且客户端将无法观察到任何更改。
假设使用的容器遵循正常的std
约定,您可以显式拼写出类型:
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;
}
}
}
在函数的第一个版本中,containerT
不是模板。它是一个类(它可以是模板的实例化,但这无关紧要)。
如果containerT
满足标准容器概念,那么你可以写:
for (typename containerT::iterator i ...) {
for (typename containerT::value_type::iterator j ...)
编译器会告诉你问题出在哪里。您正在将containerT
声明为类型,但您将其用作模板。因此,您可以尝试将containerT
更改为模板模板参数(与containerRow
相同)。
template<template<class> class containerT, template<class> class containerRowT, typename containerElementT>
void incElement(containerT<containerRowT<containerElementT> > c);
那么对于 STL 容器来说呢:
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);
}
}
}
相关文章:
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 解除分配数组会导致退出 11 代码
- 从 C++11 代码中获取系统内存和 CPU 使用率
- 我需要用c ++ 98重写c ++ 11代码,c ++ 11正在使用lambda函数[&](const Output & o
- 从远程主机中编译C 11代码的问题
- 构建 C++11 代码时 Coverity 中的内部错误
- 尝试将 C++11 代码转换为 C++03 时默认函数模板参数出错
- 如何将C++11代码编译为网络汇编
- 如何在不提升的情况下将 C++11 代码转换为 C++98
- 如何使用 Orwell Dev-C++ 编译 C++11 代码
- 非C++11程序中的C++11代码/库
- 如何让g++使用move构造函数编译c++11代码
- 有没有一种方法可以将C++11代码重构为C++代码,这些代码可以由能力较弱的编译器编译
- Sublime Text 2运行c++ 11代码
- 为什么包含rand()的c++ 11代码在多个线程中比在一个线程中慢?
- 针对Visual Studio 2008的c++ 11代码的兼容性
- 用Clang编译c++11代码的不完整类型
- 如何用android ndk和eclipse编译c++11代码
- 嵌入式 C++11 代码 — 我需要易失性代码吗?
- 如何在 DDD(或 gdb)中使用unique_ptr调试 C++11 代码