【问题标题】:How should I define this complex function template?我应该如何定义这个复杂的函数模板?
【发布时间】:2014-07-07 22:48:48
【问题描述】:

我需要定义一个函数,该函数有两个参数,第一个是一个容器,其中包含一些T类型的容器,第二个是一个整数,函数的签名可能是这样的:

#include <vector>
#include <string>
#include <list>

using namespace std;

vector<vector<T>> my_function(const vector<vector<T>>& var, int cnt);
vector<list<T>> my_function(const list<vector<T>>& var, int cnt);

我应该如何定义函数模板?

【问题讨论】:

  • @KerrekSB 不应该是 T 而不是 void 吗?
  • 嗯...如果你有勇气,你可以谷歌模板模板参数
  • @KerreSB 我返回一个向量>,但得到一个常量向量>& 作为参数怎么样?
  • 如果我理解,你有一个containertype_a 行的一个containertype_b 列的一个elementtype_c,并且你想返回一个containertype_b 的一个containertype_a 的列的一个elementtype_c 的行?基本上,切换什么是外部和什么是内部 containertype+index...您知道,使用外观和代理对象可能更容易和更快...
  • @dguan:哦,我知道你想把X的容器B的容器A换成X的容器A的容器B?

标签: c++ templates stl containers template-specialization


【解决方案1】:

这就是你说你想要的。
现在,您知道可以使用哪种容器,甚至可以用它做一些有价值的事情......

#include <vector>

template<class X> class Y{};

template<template<class outer, class... x>
  class inner, template<class element, class... x> class outer,
  class element, class... x>
inner<outer<element, x...>, x...>
my_function(const outer<inner<element>>& var, int cnt) {
  inner<outer<element, x...>, x...> ret;
  (void)var, (void)cnt;
  return ret;
}

int main() {
#if 1
    const std::vector<std::vector<int>> x;
#else
    const Y<Y<int>> x{};
#endif
    my_function(x, 0);
}

科利鲁:http://coliru.stacked-crooked.com/a/bb06e39c799e3b5e
延伸阅读:Template Template Parameters

【讨论】:

  • 这似乎正是我想要的,但它在 VS2013 中无法编译,我得到了这个错误代码:1>d:\users\dguan\documents\visual studio 2013\projects\test\test \test.cpp(197): error C2784: 'inner,x...> my_function(const outer,> &,int)' : 无法推断'const outer,> &' 的模板参数来自 'const std::vector<:vector>>,std::allocator<:vector _ty>>>>' 无论如何,谢谢,它确实给了我很好的提示,我正在努力......
  • 我期待更新您需要哪些更改才能满足 vs13。祝你玩得开心。
  • 谢谢,虽然我会在 g++ 上尝试一下。而且,由于这似乎是最接近的解决方案,如果没有更好的答案,我将很快接受它。干杯。
【解决方案2】:

您可以将函数定义为:

template<template<class> class outer,
         template<class> class inner,
         class element>
outer<inner<element>> my_function(
    const outer<inner<element>>& var, int cnt) {
  outer<inner<element>> ret;
  (void)var, (void)cnt;
  return ret;
}

但是,您不能使用以下行调用该函数:

std::vector<std::vector<int>> v;
my_function(v, 0);

my_function<std::vector, std::vector, int>(v, 0);

因为std::vector 是使用多个模板参数定义的。解决该问题的一种方法是使用:

template <typename T>
using MyVector = std::vector<T>;

然后,你可以调用:

MyVector<MyVector<int>> v;
my_function<MyVector, MyVector, int>(v, 0);

仍在尝试找出原因

MyVector<MyVector<int>> v;
my_function(v, 0);

没用。

【讨论】:

    【解决方案3】:

    我分享了一些代码,这些代码以更简洁易读的方式定义了函数。

    template <typename T>
    class MyClass {
        typedef vector <T> ContainerVecT;
        typedef list <T> ContainerListT;
    
        typedef vector <ContainerVecT> MyContainerVecT;
        typedef vector <ContainerListT> MyContainerListT;
    
        MyContainerVecT my_function(const MyContainerVecT& var, int cnt);
        MyContainerListT my_function(const MyContainerListT& var, int cnt);
    };
    

    如果不满足您的要求,请告诉我。我们会努力解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多