Hi,
I'm trying to define a map-like interface to a container.
My basic problem is that the container is associative,
but values are not stored in pairs internally.
(and as we know, the map value_type must be pair<const Key, T>)
Instead there's a list (vector) of values, and a method
to get key reference.
But I'm ready to increase the values vector to store pairs.
So... Internally, values are actually stored in a vector.
But std::vector can only store pair with non-const types.
Now I have
std::vector<std ::pair<Key, T> > m_values;
and
typedef std::pair<const Key, T> value_type;
How do I define the reference type returned from (non-const) iterators?
Trouble invites itself to the iterator dereference operator:
class iterator {
friend class MyContainer;
iterator(const ValueList* values) : m_values(values ), current(0) {}
public:
reference operator*() const { ...? }
};
Now, in boost concept checks for Forward Container (which is
required for Pair Associative and most any container types),
I get back-paddled in
reference r = *it;
I see two ways of getting past this:
(i) typedef value_type reference;
and returning a copy of the value - but this has wrong semantics:
assigning to (*iter).second doesn't change the stored value.
(ii) return *reinterpret_ca st<value_type*> (&(*m_vector)[current]);
Well, it works, sort of, but aint pretty... or portable?
Any better approach?
Should I define a custom value_type class with desired behaviour?
And if so, doyouknow some similar example I could look at?
(I've no desire to attempt to write a custom vector class that stores
pairs with const members, but perhaps one exists somewhere?)
(Of course I could make it look like anything, ie let iterator point to
mapped_type and add a method key() to access the key, or whatever,
but while it's an option, it's not the point...)
Thanks,
homsan 2 2157
On 2005-12-16, brutus <sk*******@tval fager.se> wrote: Hi,
I'm trying to define a map-like interface to a container.
The C++ type system gets in the way here, as you've found out.
My basic problem is that the container is associative, but values are not stored in pairs internally. (and as we know, the map value_type must be pair<const Key, T>) Instead there's a list (vector) of values, and a method to get key reference.
But I'm ready to increase the values vector to store pairs. So... Internally, values are actually stored in a vector. But std::vector can only store pair with non-const types. Now I have
std::vector<std ::pair<Key, T> > m_values; and typedef std::pair<const Key, T> value_type;
How do I define the reference type returned from (non-const) iterators?
I see two ways of getting past this:
(i) typedef value_type reference; and returning a copy of the value - but this has wrong semantics: assigning to (*iter).second doesn't change the stored value.
(ii) return *reinterpret_ca st<value_type*> (&(*m_vector)[current]); Well, it works, sort of, but aint pretty... or portable?
It's not pretty, and it's implementation defined, but in practice
it's portable.
Any better approach?
Do a Google Groups search for "const_cast std::pair" for some
interesting previous discussion of this topic.
Should I define a custom value_type class with desired behaviour?
std::map, facing the same problem when implementing its insert
function, makes temporaries. You could do that too, though it
seems considerably more horrible to do in the operator* of
an iterator than in map::insert.
And if so, doyouknow some similar example I could look at?
It's a big obstacle to a nice vector-based associative container,
but I don't know of any commonly accepted solution.
--
Neil Cerutti
Neil Cerutti wrote: I'm trying to define a map-like interface to a container. The C++ type system gets in the way here, as you've found out.
A little strange, as this must've been known and understood
at the time of standardization - it seemed a side comment in
one of my books hints that the std::pair template constructor
was defined specifically, or partly, to handle map insertion.
If we could just have (*iter).first() and (*iter).second( )
(or any of a host of other notations) there wouldn't be a problem at all... My basic problem is that the container is associative, but values are not stored in pairs internally.
(ii) return *reinterpret_ca st<value_type*> (&(*m_vector)[current]); Well, it works, sort of, but aint pretty... or portable?
It's not pretty, and it's implementation defined, but in practice it's portable.
- so good enough to use at home (if it works), not good enough to
show around. Any better approach?
(well, if I want to be safer but no less sorry, iterators can
of course be immutable. Or change value_type. After all it's not
exactly a map, just an associative damn container.)
Do a Google Groups search for "const_cast std::pair" for some interesting previous discussion of this topic.
Thanks, that got me some relevant hits!
And apparently a well-known problem. Darn.
I tried a few searches, but didn't find the right phrase
(>50k hits, mostly source files and local copies of SGI docs)
It's a big obstacle to a nice vector-based associative container, but I don't know of any commonly accepted solution.
So for once I'm not _just_ missing some basic syntax rule,
but have a real problem. Not sure if I like it better out here...
Thanks for your comments!
Bruno This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: richard.forrest1 |
last post by:
I have a problem with an abstract interface class whose implementation
classes need to return different iterator types (but with the same
value_types etc).
Classes A and B both conform to the same abstract Interface class. Interface
has a pair of virtual functions begin() and end() that generate a typical
STL style range. Classes A and B provide different implementations, maybe
using different types of container. The problem is that, to...
|
by: richard.forrest1 |
last post by:
I have a problem with an abstract interface class whose implementation
classes need to return different iterator types (but with the same
value_types etc).
Classes A and B both conform to the same abstract Interface class. Interface
has a pair of virtual functions begin() and end() that generate a typical
STL style range. Classes A and B provide different implementations, maybe
using different types of container. The problem is that, to...
|
by: Merlin |
last post by:
Hi,
I am a C++ developer and would like to implement container classes for
various types of objects. I use MS Visual C++ to compile my code.
Nevertheless I like to write code that is independent from MFC and
intentionally I am trying to avoid inheriting from CList and CArray
classes. Recently, I became familiar with iterators and have read many
articles including the GOF description on what they help to achieve.
My understanding so far...
|
by: desktop |
last post by:
I am not sure I quite understand the use of iterators. I have this int
array:
int a = {1,2,3,4,5}
I would now like to make an iterator to the first and last element:
std::iterator<intiter;
iter = a;
|
by: desktop |
last post by:
1)
I have this code:
std::list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_back(4);
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |