Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class with template container

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.

like image 820
DuXeN0N Avatar asked May 16 '13 20:05

DuXeN0N


People also ask

What is a container template?

Service templates can contain other service templates. Any template that contains other templates is called a "container" template. Container templates can hold VM, PM, and storage templates, as well as other container templates.

What is template class and template function?

Function templates. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What is the difference between class template and template class?

A class template is a template that is used to generate classes whereas a template class is a class that is produced by a template.


1 Answers

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.

like image 131
Andy Prowl Avatar answered Sep 26 '22 09:09

Andy Prowl