Home how to define container templated with enum
Reply: 1

how to define container templated with enum

Artem Published in 2017-12-07 17:05:02Z

I need to create a container which holds integer and templated with enum.

 enum Color{R,G,B};

 template<class Color C,//class before color will be removed
 template <class, class = allocator<int>> class Container>
 class MyClass
    Container<int> buffer;

I need to create vector and list out of it. Something like:

 MyClass<Color::R, std::list> mbs
 MyClass<Color::G, std::vector> mbs

 //wrong number of template arguments (1, should be 2)
 MyClass<Color C, vector> v1;

 //for contaner
 Container<int>::iterator nth = buffer.begin()
AndyG Reply to 2017-12-07 18:56:04Z

You're looking for a non-type template parameter

For your container it would be defined like so:

template<Color C, 
         template<class, class = std::allocator<int>> class Container>
class MyClass{
   Container<int> buffer;
   // ...

You can create an instance like so:

MyClass<Color::R, std::vector> instance;

You can create an iterator like so:

typename Container<int>::iterator iter = buffer.begin();

typename is required because iterator is a dependent name

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321678 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO