make a common interface to boost::array<..., maxsize> and std::vector<...>

H

Hicham Mouline

Hi,

I have a situation where behind the scenes I implement a container either as
1. boost::array<type, max_size> + size_t current_size
this array would be contained in some class "Container" that behaves as
closely as possible to
standard containers, with the addition that it only considers elements
between 0 and current_size-1
so that
size() returns current_size ( can be variable )
unchecked [] returns the usual
begin() returns &...[0]
end() returns &...[current_size-1]
I would make a new random access iterator that takes into account
current_size

I would be guaranteed this is on the stack

2. std::vector<type>
This may and probably is on the heap.


I would like to access both types with the same interface such that:

template <typename traits>
class MyClass {

// member container
// depending on Traits
Container<traits::max_size> data_; // this "is" either 1 or 2

// implementation
// data_.begin()
// data_.end()
// std::lower_bound( data_.begin(), data_.end(). .....)
};

and I could access data_ as if it was a "standard" container. Basically I
wanna hide the current_size case when Container is array<>,
note its max size is available in Traits. if max is 0 we use vector<>
otherwise array<>

I have then a primary template for Container (array<>) and an explicit
specialization on 0 (vector<>)
this forces me to rewrite all vector<> desired operations in Container as
forwarding operations that are just 1 liners,
which i think is a waste. In other words, I wish Container<0> to "be"
vector<>, but it is understood using inheritance is
woudl cause undefined behavior, in particular in case of virtual
destruction.

regards,
 
V

Vaclav Haisman

Hicham Mouline wrote, On 6.7.2009 19:17:
Hi,

I have a situation where behind the scenes I implement a container either as
1. boost::array<type, max_size> + size_t current_size
this array would be contained in some class "Container" that behaves as
closely as possible to
standard containers, with the addition that it only considers elements
between 0 and current_size-1
so that
size() returns current_size ( can be variable )
unchecked [] returns the usual
begin() returns &...[0]
end() returns &...[current_size-1]
I would make a new random access iterator that takes into account
current_size

I would be guaranteed this is on the stack

2. std::vector<type>
This may and probably is on the heap.


I would like to access both types with the same interface such that:
[...]
Why don't you typedef the container in the traits then?
 
H

Hicham Mouline

Vaclav Haisman said:
Hicham Mouline wrote, On 6.7.2009 19:17:
Hi,

I have a situation where behind the scenes I implement a container either
as
1. boost::array<type, max_size> + size_t current_size
this array would be contained in some class "Container" that behaves as
closely as possible to
standard containers, with the addition that it only considers elements
between 0 and current_size-1
so that
size() returns current_size ( can be variable )
unchecked [] returns the usual
begin() returns &...[0]
end() returns &...[current_size-1]
I would make a new random access iterator that takes into account
current_size

I would be guaranteed this is on the stack

2. std::vector<type>
This may and probably is on the heap.


I would like to access both types with the same interface such that:
[...]
Why don't you typedef the container in the traits then?

That wouldn't work
I have case 1 is (array<> + current size) and case 2 (vector<>)
I could typedef array<> or vector<> to ContainerType for e.g., but I'd still
have the current size to handle explicitly.

rds,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top