如何从输出迭代器中获取值类型? [英] How to get the value type from an output iterator?
问题描述
假设我有一个C容器(例如 MyContainer
),其中包含的对象存储为 void *
指针。遍历此容器元素的唯一方法是通过两个接口函数:
Let's say that I have a C container (e.g., MyContainer
) with contained objects stored as void*
pointers. The only way to iterate through the elements of this container is via two interface functions:
-
getFirstElem(MyContainer const& ,void *)
:输出容器的第一个元素。 -
getNextElem(MyContainer const&,void *)
:输出容器的下一个元素。
getFirstElem(MyContainer const&, void*)
: Outputs the first element of the container.getNextElem(MyContainer const&, void*)
: Outputs the next element of the container.
我想编写循环函数来迭代这个C的元素容器通过上面提到的接口函数并将它们的值复制到C ++容器中(例如 std :: vector
)。
I want to code a generic function that iterates through the elements of this C container via the interface functions mentioned above and copy their values into a C++ container (e.g. std::vector
).
到目前为止我做了什么:
What I've done so far:
template<typename OutputIterator>
void
copy_container(MyContainer const &cont, OutputIterator first) {
typename std::iterator_traits<OutputIterator>::value_type elem;
if(getFirstElem(cont, &elem)) {
do {
*first = elem;
++first;
} while(getNextElem(cont, &elem))
}
}
以上示例适用于普通迭代器。但是,它无法使用输出迭代器进行编译(例如, copy_container(cont,std :: back_inserter(myvector));
)。
The above example works OK with normal iterators. However, it fails to compile with output iterators (e.g., copy_container(cont, std::back_inserter(myvector));
).
原因是 std :: iterator_traits :: value_type
在参数的情况下导致 void
type是一个输出迭代器。
The reason is that std::iterator_traits::value_type
results in void
in cases where the argument type is an output iterator.
有没有办法让这个泛型函数也适用于输出迭代器?
我知道在C ++ 11中可以使用 decltype
来完成(例如, decltype(* first) )
),但我对C ++ 11之前的解决方案特别感兴趣,因为我使用旧的C ++编译器(gcc v4.4.7)。
I know that in C++11 it could be done by using decltype
(e.g., decltype(*first)
), but I'm particularly interested in pre-C++11 solutions since I use an old C++ compiler (gcc v4.4.7).
推荐答案
正确观察,输出迭代器的 value_type
是 void
。所以除了替换它之外别无选择:
As correctly observed, the value_type
of an output iterator is void
. So there not much to do apart from replacing this :
typename std::iterator_traits<OutputIterator>::value_type elem;
这个
decltype(*first) elem;
(即使标准不保证它也能正常工作 - 解除引用可能会返回代理输出迭代器)。
(even though the Standard doesn't guarantee it'll work - a proxy might be returned by dereferencing an output iterator).
正如你所说的没有C ++ 11解决方案所以可能需要进行重新设计。以下是一些选项:
As you said no C++11 solution so a redesign might be needed. Here are some options:
您可以传递对容器的引用,而不是第一个元素的迭代器。看起来你想要的只是一个 push_back
。
Instead of an iterator to the first element, you could pass a reference to the container. It seems like all you want is a push_back
.
template<template<typename,typename> class stlContainer>
void copy_container(
MyMontainer const &cont, OutputIterator first)
{
// insertion in stlContainer
然后您需要的是一层特征,以便调度到每个容器的正确插入实现
then all you need is a layer of traits to dispatch to the right implementation of insertion per container
值类型可以是额外的模板参数。
The value type could be an extra template parameter.
template<typename value_type, typename OutputIterator>
void copy_container(MyMontainer const &cont, OutputIterator first)
{
value_type elem;
...
这篇关于如何从输出迭代器中获取值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!