【问题标题】:Templating a function to deduce the return type stl-container from input arguments模板化函数以从输入参数推断返回类型 stl-container
【发布时间】:2017-07-25 20:01:20
【问题描述】:

我有一个函数,其目标是获取特定数据类型的 stl 容器并返回不同特定数据类型的相同 stl 容器。见下文:

template <
template <typename, typename = std::allocator<double> > class ReturnContainer,
template <typename, typename = std::allocator<int> > class Container
    >
inline ReturnContainer<double> transform(const Container<int>& container)
{
  ReturnContainer<double> ret(container.size());

  for(size_t i = 0; i < container.size(); ++i)
  {
    // Do something here
  }
  return ret;
}

你可以用作:

//
// Using vectors
//
std::vector<int> data_vector_in;
data_vector_in.push_back(0.0);
data_vector_in.push_back(1.0);

std::vector<double> data_vector_out = transform<std::vector>(data_vector_in);

//
// Using lists
//
std::list<int> data_list_in;
data_list_in.push_back(0.0);
data_list_in.push_back(1.0);

std::list<double> data_list_out = transform<std::list>(data_list_in);

一个人(或一个人)怎么能这样写,以便可以从输入容器中推断出返回容器?

std::vector<double> data_vector_out = transform(data_vector_in); // No <std::vector>

虽然这个问题的精神类似于The std::transform-like function that returns transformed container,但最流行的解决方案指向我需要特别避免的模板元编程。使用可变参数模板参数的方案解决了这个问题。

【问题讨论】:

  • 提示:为什么需要ReturnContainer?
  • @Barry 如果我尝试在返回输出上使用Container,它可能是无效的,因为double 的分配器不是int。以此为例,当我不通过用户定义的类而不是 double 和 int 处理本机类型时,情况会变得更糟。
  • @OttoNahmee 你确定它无效吗?
  • 发生这种情况是因为您为第二个模板参数提供了std::allocator&lt;int&gt; 的默认模板参数。因此,当你写Container&lt;double&gt; 时,就好像你写了Container&lt;double, std::allocator&lt;int&gt;&gt;。您当前的模板模板参数还有一个问题,您不能假设容器有两个模板参数,因为std::map&lt;&gt; 和std::set&lt;&gt; 等容器有两个以上;您的模板模板参数应该采用可变参数模板。

标签: c++ templates stl containers c++14


【解决方案1】:

我之所以回答,是因为人们在 cmets 中所做的暗示似乎对您没有帮助。

对于具有与其关联的单一类型(例如vector&lt;int&gt;)的向量容器(例如vector 和list,而不是map),您可以接受可变参数模板模板参数以匹配容器,但您还必须记住,这些容器的模板定义通常包含类型以外的内容。例如,分配器(很少使用,但允许您对内存分配进行一些控制)。

我们经常忘记Allocator 等,因为这些通常是默认的。

这是您的新界面:

template <template<class...> class Container, class... Args>
Container<double> transform(const Container<int, Args...>& container);

您明确表示要匹配 int 的容器并转换为 double 的容器,所以我在这里复制了它。

Args... 将匹配分配器(以及类型之后的任何其他模板参数)。我们可以忽略在返回类型中指定分配器,因为它和它后面的其他参数通常是默认的。

现在你可以这样称呼它:

// Using vectors
std::vector<int> data_vector_in{0, 1};
auto data_vector_out = transform(data_vector_in);

// ensure we got the right type back
static_assert(std::is_same<decltype(data_vector_out), std::vector<double>>::value, "Err");

// Using lists
std::list<int> data_list_in{0, 1};
auto data_list_out = transform(data_list_in);
static_assert(std::is_same<decltype(data_list_out), std::list<double>>::value, "Err");

Live Demo (C++14)

【讨论】:

    【解决方案2】:

    为什么不只使用构造函数?

    std::vector<int> data_vector_in;
    std::vector<double> data_vector_out(data_vector_in.begin(), data_vector_in.end());
    
    std::list<int> data_list_in;
    std::list<double> data_list_out(data_list_in.begin(), data_list_in.end());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多