473,406 Members | 2,352 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,406 software developers and data experts.

set of pointers

REH
Is it well defined to use a std::set of pointers? I ask because of
the restrictions on using relational operators with pointers. Does a
set of pointers suffer from the same restrictions, or does the
standard require it to be supported. I can't find any information one
way or the other in the standard or from other sources.

Thanks,
REH
Feb 26 '08 #1
10 2846
REH wrote:
Is it well defined to use a std::set of pointers? I ask because of
the restrictions on using relational operators with pointers. Does a
set of pointers suffer from the same restrictions, or does the
standard require it to be supported. I can't find any information one
way or the other in the standard or from other sources.

Thanks,
REH
std::set of pointers are well supported, pointers have value semantic,
comparable.

The relational operators you refer work fine because pointers can be
compared by value.

Fei
Feb 26 '08 #2
On Feb 26, 10:31*am, REH <spamj...@stny.rr.comwrote:
Is it well defined to use a std::set of pointers? *I ask because of
the restrictions on using relational operators with pointers. *Does a
set of pointers suffer from the same restrictions, or does the
standard require it to be supported. *I can't find any information one
way or the other in the standard or from other sources.

Thanks,
REH
Since pointers implement operator< as required by std::set, then it is
possible to use pointers.
Note that what is being compared is the actual address being stored in
the pointer, and not what it points to.

Lance
Feb 26 '08 #3
REH wrote:
Is it well defined to use a std::set of pointers? I ask because of
the restrictions on using relational operators with pointers. Does a
set of pointers suffer from the same restrictions, or does the
standard require it to be supported. I can't find any information one
way or the other in the standard or from other sources.
In addition to what everyone here has discussed so far, you can provide
a custom comparison functor for your set, so you can sort it based on
what the pointers refer to, rather than the raw pointers themselves.
Feb 26 '08 #4
REH
On Feb 26, 10:45 am, Lance Diduck <lancedid...@nyc.rr.comwrote:
Since pointers implement operator< as required by std::set, then it is
possible to use pointers.
Note that what is being compared is the actual address being stored in
the pointer, and not what it points to.

Lance
Thanks for replying, but you are not correct. Pointers are not
necessarily addresses, and the standard only allows pointers to be
relationally compared if they come from the same object (i.e., the
same struct or array).

REH
Feb 26 '08 #5
REH
On Feb 26, 10:44 am, Fei Liu <fei....@gmail.comwrote:
std::set of pointers are well supported, pointers have value semantic,
comparable.

The relational operators you refer work fine because pointers can be
compared by value.

Fei
Your logic doesn't hold. Pointers can only be relationally compared
if they are from the same struct, array, etc.

REH
Feb 26 '08 #6
On Feb 26, 4:44 pm, Fei Liu <fei....@gmail.comwrote:
REH wrote:
Is it well defined to use a std::set of pointers? I ask
because of the restrictions on using relational operators
with pointers. Does a set of pointers suffer from the same
restrictions, or does the standard require it to be
supported. I can't find any information one way or the
other in the standard or from other sources.
std::set of pointers are well supported, pointers have value
semantic, comparable.
The relational operators you refer work fine because pointers
can be compared by value.
Pointers are only comparable for inequality (e.g. <) if they
point into the same object or array. You can't expect to
compare two arbitrary pointers and get anything meaningful. (It
does actually work on a lot of machines, but not always on Intel
architectures.)

You can use pointers in std::set, however, since std::set
doesn't use the < operator, but rather std::less, and the
standard requires the implementation to make this work, even if
the < operator doesn't work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 26 '08 #7
REH wrote:
On Feb 26, 10:44 am, Fei Liu <fei....@gmail.comwrote:
>std::set of pointers are well supported, pointers have value semantic,
comparable.

The relational operators you refer work fine because pointers can be
compared by value.

Fei

Your logic doesn't hold. Pointers can only be relationally compared
if they are from the same struct, array, etc.

REH
It's implied that your 'pointers' are of the same type. Otherwise your
question is ill formed. I was going to mention this but it's good that
you understand that you must use same type of pointers in a single set.

Fei
Feb 26 '08 #8
Fei Liu wrote:
REH wrote:
>On Feb 26, 10:44 am, Fei Liu <fei....@gmail.comwrote:
>>std::set of pointers are well supported, pointers have value
semantic, comparable.

The relational operators you refer work fine because pointers can be
compared by value.

Fei

Your logic doesn't hold. Pointers can only be relationally compared
if they are from the same struct, array, etc.

REH

It's implied that your 'pointers' are of the same type. Otherwise your
question is ill formed. I was going to mention this but it's good that
you understand that you must use same type of pointers in a single
set.
Pointers can only be compared using relational operators if the objects
they point to belong the same [larger] object, for example, an array.
If they pointers have the same type but aren't part of another, larger,
object, using relational operators on them is undefined.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 26 '08 #9
REH
On Feb 26, 4:21 pm, Fei Liu <fei....@gmail.comwrote:
It's implied that your 'pointers' are of the same type. Otherwise your
question is ill formed. I was going to mention this but it's good that
you understand that you must use same type of pointers in a single set.

Fei
No, it has nothing to do with being the same type. Pointers must
point to elements or fields of the same OBJECT to be relationally
comparable.

REH
Feb 26 '08 #10
On Feb 26, 10:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Fei Liu wrote:
REH wrote:
On Feb 26, 10:44 am, Fei Liu <fei....@gmail.comwrote:
std::set of pointers are well supported, pointers have value
semantic, comparable.
>The relational operators you refer work fine because
pointers can be compared by value.
Your logic doesn't hold. Pointers can only be relationally
compared if they are from the same struct, array, etc.
It's implied that your 'pointers' are of the same type.
Otherwise your question is ill formed. I was going to
mention this but it's good that you understand that you must
use same type of pointers in a single set.
Pointers can only be compared using relational operators if
the objects they point to belong the same [larger] object, for
example, an array. If they pointers have the same type but
aren't part of another, larger, object, using relational
operators on them is undefined.
And if they're of different types, the compiler will try to find
a common type, and use that. (I'm not sure of the exact rules,
because I can't think of a general case where it would be
useful. But cv qualifiers can definitely be discarded---you can
compare a char const* with a char* without any problems, as long
as they point into the same object.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
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...

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.