473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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_cast, 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_cast (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 1949
weaknessforcats
9,208 Expert Mod 8TB
Have you considered returning a list<Edge> rather than list<Edge*>& ?

That would protect the original list.
Sep 25 '08 #2
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_cast, 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
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
Any other ideas, before I create my own custom iterator?
Oct 2 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
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
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
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);...
6
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...
4
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>....
7
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...
45
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.