简体   繁体   English

我可以退回不同类型的模板化容器吗?

[英]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.

相关问题 从异构模板值的容器返回模板类型 - Return templated type from container of heterogeneous templated values 如何在容器中存储不同类型的模板对象? - How to store templated objects of different type in container? 如何从模板化类方法返回依赖类型? - How can I return a dependent type from templated class method? 如何使模板化运算符推导出正确的返回类型? - How can I make templated operator deduce correct return type? C ++模板化类定义 - 不同类的模板化返回类型 - C++ Templated Classes Definition - Templated Return Type of Different Class 我无法让这个虚拟模板化仿函数运算符返回正确的类型 - I can't get this virtual templated functor operator to return the correct type 如何使用SFINAE确定非模板化函数的优先级,同时还提供返回类型? - How can I use SFINAE to prioritise a non-templated function while also providing a return type? 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class? 我可以根据类型ID实现其签名只有不同的模板化函数吗? - Can I Implement templated functions whose signatures are only different based on a type id? 模板函数重载返回类型 - Templated functions overload return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM