3

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?

dguan
  • 1,023
  • 1
  • 9
  • 21
  • @KerrekSB shouldn't it be rather T than void ? – Christophe Jul 07 '14 at 22:52
  • Well...if you are brave, you could google template template-parameters – StoryTeller - Unslander Monica Jul 07 '14 at 22:54
  • @KerreSB how about I am returning a vector>, but getting a const vector>& as parameter? – dguan Jul 07 '14 at 23:08
  • If I understand, you have a containtertype_a rows of a containertype_b columns of an elementtype_c, and you want to return a containertype_b of columns of a containertype_a of rows of an elementtype_c? Basically, switch what is outer and what is inner containertype+index... You know, it might be easier and faster to use a facade and proxy-objects... – Deduplicator Jul 07 '14 at 23:11
  • @dguan: Oh, I see do you want to swap a container A of container B of X into a container B of container A of X? – Kerrek SB Jul 07 '14 at 23:12
  • @Deduplicator I am working on a project which takes a vector>, and returning a vector>, I am thinking that, as the algorithm can also work on char, long, float, string, etc, and can also work on a list, so I would like to define a generic template function. – dguan Jul 07 '14 at 23:18

3 Answers3

1

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

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • This seems just what I want, but it does not compile in VS2013, and I got this error code: 1>d:\users\dguan\documents\visual studio 2013\projects\test\test\test.cpp(197): error C2784: 'inner,x...> my_function(const outer,> &,int)' : could not deduce template argument for 'const outer,> &' from 'const std::vector>,std::allocator>>>' Thanks anyway, and it did give me good hints, and I am working on it...... – dguan Jul 09 '14 at 07:04
  • I look forward to an update what changes you need to satisfy vs13. Wish you luck fiddling with it. – Deduplicator Jul 09 '14 at 11:56
  • Thank you, mean while I will try it on g++. And, as this seems the most close solution, I'm going to accept it shortly if there's no better answer coming in. Cheers. – dguan Jul 09 '14 at 13:40
0

You can define the function as:

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

However, you cannot call the function with the following lines:

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

or

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

since std::vector is defined using more than one template parameter. One work around to that problem is to use:

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

Then, you can call:

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

Still trying to figure out why

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

doesn't work.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

I have shared some code which defined the functions in a neater and readable manner.

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

Please let me know if it does not satisfy your requirement. We will work on that.

Abhishek Mittal
  • 356
  • 1
  • 16