473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need efficient way of converting list<A*> to list<const A*>

4 New Member
I am working with a specialized graph class, which has a node class and an edge class. In the node class, there is a list of pointers to each edge. Code that uses the graph must be able to see what edges are contained in a node. This is handled by returning a const reference to a list of edge pointers, i.e:
Expand|Select|Wrap|Line Numbers
  1. const list<Edge*>& Node::getEdges() const {
  2.     return edges; // list<Edge*>
  3. }
  4.  
However, I don't like this because code outside the graph can modify the edges through the edge pointers. So, ideally, I want this:

Expand|Select|Wrap|Line Numbers
  1. const list<const Edge*>& Node::getEdges() const {
  2.     return edges; // list<Edge*>
  3. }
  4.  
But, this of course won't work because list<const Edge*> is a different type than list<Edge*>. I had been handling this problem with a reinterpret_cas t, which theoretically should work fine since list<const Edge*> and list<Edge*> really are the same type, just with different names. However, this violates strict aliasing rules of g++, so I'm concerned that -03 optimized code may not always work.

Does anyone know a creative way of converting a list<A*> to a list<const A*> without using a reinterpret_cas t (and without less efficient methods like copying the entire list)?

I was considering creating my own Java-like Iterator that only returns a const version of each object in the list. Although this would work, it seems like overkill. Any suggestions would be appreciated. Thanks.
Sep 25 '08 #1
6 1971
weaknessforcats
9,208 Recognized Expert Moderator Expert
Have you considered returning a list<Edge> rather than list<Edge*>& ?

That would protect the original list.
Sep 25 '08 #2
gsBytes
4 New Member
I have considered that, but each node already has a list of Edge pointers, and I am trying to avoid having to copy all the data into a new list. I was also trying to eliminate returning a copy of the list, since this occurs in code that is performance critical.

Also, there are other places in the code with similar problems. For example, consider two classes Base and Derived, where Derived inherits Base. There are places in the code where a class contains a list of Derived pointers, but has a method that needs to return a list of Base pointers.

Expand|Select|Wrap|Line Numbers
  1. const list<Base*>& SomeClass::func() const {
  2.     return derivedList; // list<Derived*>
  3. }
  4.  
Again, I was using reinterpret_cas t, but I'm not happy with it. Is there any better way to do this without having to copy the entire list into a new list?
Sep 25 '08 #3
gsBytes
4 New Member
By the way, just to clarify, I'm not just concerned with changes to the list. I am also trying to avoid changes to the objects that are pointed to by the pointers in the list.
Sep 25 '08 #4
gsBytes
4 New Member
Any other ideas, before I create my own custom iterator?
Oct 2 '08 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
My last suggestion is to return a list<const Edge*>. That is, if you can return an Edge* you can always assign an Edge* to a const Edge*. Then you add the const Edge* to the list and return the list.

No there can be no changes to anything pointed at by a const Edge*.
Oct 4 '08 #6
Patrick Horgan
2 New Member
This doesn't violate strict-aliasing. Things that are compatible types or differ only by the addition of any combination of signed, unsigned, const or volatile are allowed to alias. If you want to know more about strict-aliasing and what it is you can look at my white paper, Understanding C/C++ Strict Aliasing
or – Why won't the #$@##@^% compiler let me do what I need to do!


Patrick
Dec 30 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2392
by: Alexander Malkis | last post by:
//Consider: class A { /*...*/ }; template<class T> class list {/*... */ }; void f(const list<const A*> lst) { /*...doesn't change the arg...*/ } void g(list<A*> lst) { f(lst); //Intuitively ok, but compiler rejects. }
6
6663
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
4
53016
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
7
57556
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
45
18910
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
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...
1
10190
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,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
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...
0
5447
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...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
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
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.