42

How can I declare template class (adaptor) with different containers as template arguments? For example, I need to declare class:

template<typename T, typename Container>
class MyMultibyteString
{
    Container buffer;
    ...
};

And I want it to my based on vector. How to make it hard-defined? (to prevent someone from writing such declaration MyMultibyteString<int, vector<char>>).

Moreover, how to implement such construction:

MyMultibyteString<int, std::vector> mbs;

without passing template argument to container.

DuXeN0N
  • 1,577
  • 1
  • 14
  • 29

3 Answers3

83

You should use template template parameters:

template<typename T, template <typename, typename> class Container>
//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
class MyMultibyteString
{
    Container<T, std::allocator<T>> buffer;
    // ...
};

This would allow you to write:

MyMultibyteString<int, std::vector> mbs;

Here is a compiling live example. An alternative way of writing the above could be:

template<typename T,
    template <typename, typename = std::allocator<T>> class Container>
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
class MyMultibyteString
{
    Container<T> buffer; // <== No more need to specify the second argument here
    // ...
};

And here is the corresponding live example.

The only thing you have to pay attention to is that the number and type of arguments in the template template parameter declaration must match exactly the number and type of arguments in the definition of the corresponding class template you want to pass as a template argument, regardless of the fact that some of those parameters may have default values.

For instance, the class template std::vector accepts two template parameters (the element type and the allocator type), although the second one has the default value std::allocator<T>. Because of this, you could not write:

template<typename T, template <typename> class Container>
//                             ^^^^^^^^
//                             Notice: just one template parameter declared!
class MyMultibyteString
{
    Container<T> buffer;
    // ...
};

// ...

MyMultibyteString<int, std::vector> mbs; // ERROR!
//                     ^^^^^^^^^^^
//                     The std::vector class template accepts *two*
//                     template parameters (even though the second
//                     one has a default argument)

This means that you won't be able to write one single class template that can accept both std::set and std::vector as a template template parameter, because unlike std::vector, the std::set class template accepts three template parameters.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 2
    What a great, thorough answer. – Scott Jones May 16 '13 at 20:31
  • @ScottJones: Glad you found it useful :) – Andy Prowl May 16 '13 at 20:35
  • 4
    @ScottJones Regarding your statement: `This means that you won't be able to write one single class template that can accept both std::set and std::vector`: Would variadic templates solve the problem? http://stackoverflow.com/a/20499809/2436175 – Antonio Dec 10 '13 at 21:52
  • @Antonio: Yep, that's a possibility. Not sure why I haven't thought of that :) – Andy Prowl Dec 10 '13 at 21:58
  • 2
    "This means that you won't be able to write one single class template that can accept both `std::set` and `std::vector`...because `std::set` accepts three template parameters" - What about variadic templates? `template< class... Args >` – iPherian Oct 31 '16 at 13:27
12

Another approach to solve this is by using variadic templates and with that you can use any container as suggested in comments above and here is the implemenation :

template<template <typename... Args> class Container,typename... Types>
class Test
{
    public:
    Container<Types...> test;

};
int main()
{
  Test<std::vector,int> t;
  Test<std::set,std::string> p;
  return 0;
}
PapaDiHatti
  • 1,841
  • 19
  • 26
1

If you look at the definitions of list and vector from cplusplus.com, for example they are:

template < class T, class Alloc = allocator<T> > class list;

and

template < class T, class Alloc = allocator<T> > class vector;

So this should go as the type of the container, the other template parameter is the type of the elements. As an example this program will output 3:

#include <iostream>
#include <list>
using namespace std;

template <template <typename...> typename C, typename T>
struct Cont {
    C<T> info;
};
int main(void)
{
    Cont<list, int> cont;

    cont.info.push_back(3);
    cout << cont.info.front() << endl;

    return 0;
}
baz
  • 1,317
  • 15
  • 10