[英]Can I return a templated container of different type?
How can I create a function which behaves like this? 如何创建一个行为如此的函数? I can't seem to declare the return type correctly. 我似乎无法正确声明返回类型。
template <typename C, typename T0, typename T1>
typename C<T1>
convert_container(const C<T0>& container, T1 value) {
C<T1> new_container;
// Do some stuff...
return new_container;
}
std::vector<int> vec0;
const auto& vec1 = convert_container(vec0, float(2.0f)); // Produce a vector of floats
std::list<int> lst0;
const auto& lst1 = convert_container(lst0, float(2.0f)); // Produce a list of floats
The correct way is to use template template parameter: 正确的方法是使用模板模板参数:
C++11: C ++ 11:
template <template<typename...> class C, typename T0, typename T1> C<T1> convert_container(const C<T0>& container, T1 value) { C<T1> new_container; // Do some stuff... return new_container; }
C++03 (with allocator rebind): C ++ 03(带分配器重新绑定):
template <template<typename, typename> class C, typename T0, typename T1, typename Alloc> C<T1, typename Alloc::template rebind<T1>::other> convert_container(const C<T0, Alloc>& container, T1 value) { C<T1, typename Alloc::template rebind<T1>::other> new_container; // Do some stuff... return new_container; }
C++03 (without rebind): C ++ 03(没有重新绑定):
template <template<typename, typename> class C, typename T0, typename T1, typename Alloc> C<T1, std::allocator<T1> > convert_container(const C<T0, Alloc>& container, T1 value) { C<T1, std::allocator<T1> > new_container; // Do some stuff... return new_container; }
The problem you're running into is that the way you're trying to use C
, it isn't a simple type, but actually a class template. 你遇到的问题是你尝试使用C
,它不是一个简单的类型,而是一个类模板。 You want a template-template parameter: 你想要一个模板模板参数:
template<template<typename> class C, typename T0, typename T1>
C<T1> convert_container( C<T0> const &container, T1 value ) {
C<T1> new_container;
// Do some stuff...
return new_container;
}
Note that this won't typically work properly with standard containers such as vector
, since they take more than one template argument (although some of them are defaulted). 请注意,这通常不适用于标准容器(如vector
,因为它们需要多个模板参数(尽管其中一些是默认的)。 If you're working in C++11, you can do as @Jarod42 suggests and use template<typename...>
instead of template<typename>
. 如果你在C ++ 11中工作,你可以像@ Jarod42建议那样使用template<typename...>
而不是template<typename>
。
Hmm, your version looks a bit unclean. 嗯,你的版本看起来有点不洁净。 I'd rather go with this approach: 我宁愿采用这种方法:
#include <iterator>
template <class TargetContainer, class OriginalContainer>
TargetContainer convert_container(const OriginalContainer& inputContainer)
{
TargetContainer result;
auto inserter = std::inserter(result, result.begin());
for (auto& item: inputContainer) {
*inserter = item;
}
return result;
}
..which can be used like: ..可以使用像:
std::vector<int> vectorOfInts {1, 2, 3, 4};
auto vectorOfFloats = convert_container<std::vector<float>>(vectorOfInts);
auto listOfDoubles = convert_container<std::list<double>>(vectorOfFloats);
auto setOfLongsWithCustomAlocator = convert_container<std::set<long, myComparator, myAlocator>>(...); //etc
If you don't want to use any C++11 just replace the range based for in convert_container
with a normal for loop and the auto
declaration with it's proper type. 如果您不想使用任何C ++ 11,只需使用普通for循环替换convert_container
的范围,并使用正确的类型替换auto
声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.