Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I define this complex function template?

I need to define a function, the function takes two parameters, the first is a container which contains some containers of type T, the second is an integer, the function's signature may look like this:

#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);

how should I define the function template?

like image 865
dguan Avatar asked Dec 06 '25 16:12

dguan


1 Answers

This does what you say you want.
Now, you know what kind of containers might be used, and you might even be able to do something worthwhile with it...

#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);
}

Coliru: http://coliru.stacked-crooked.com/a/bb06e39c799e3b5e
Further reading: Template Template Parameters

like image 114
Deduplicator Avatar answered Dec 08 '25 05:12

Deduplicator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!