473,721 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rvalues and constant reference to a pointer ?

I know this is something fundamental and I ought to have known it, but
somehow this seems to be confusing me a lot.

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

class A
{};

1> A* foo(A* & ptr){ } // non-const reference to a pointer

2> A* foo( const A* & ptr) { } //reference to a pointer to const A

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...b ut it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.

But then how does one realy have a constant reference to a pointer in
this case ? const usually binds to the left(unless there is nothing to
the left, so then it binds to the right)...so in that case it would be
the same as declaration no 3.

which would mean that in reality, there is nothing as a const reference
to a pointer.

Thanks.

Jul 23 '05 #1
6 1841
Me
> 1> A* foo(A* & ptr){ } // non-const reference to a pointer
2> A* foo( const A* & ptr) { } //reference to a pointer to const A
3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...b ut it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.


Rewrite your examples using typedefs:

typedef A * Ap;
typedef const A * C_Ap;

1> Ap foo(Ap &ptr);
2> Ap foo(C_Ap &ptr);
3> Ap foo(const Ap &ptr);

Still confused?

4> Ap foo(const C_Ap &ptr);

also works. Get yourself a copy of the standard and read section 4.4
about const safety to see why (it's hard to see why here, but if you
write it out without the typedefs, it's easier).

Jul 23 '05 #2
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A


Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).

This explains why your code samples work with this version,
and makes the rest of your message moot.

Jul 23 '05 #3
Me
> > 3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).


His comment is right. A * & const would be a const reference to a
pointer to A (which is meaningless:
http://www.parashift.com/c++-faq-lit...html#faq-18.7).

Jul 23 '05 #4
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A


Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference". The above is a reference to
"A* const", just like "A* const *" would be a (non-const) pointer to
"A* const".

Jul 23 '05 #5
Rolf Magnus wrote:
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A
Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference".


A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )
The above is a reference to "A* const"


In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.

The OP did ask, "how does one get a constant reference
to a pointer". (to which the answer is, his #3).

Jul 23 '05 #6
Old Wolf wrote:
Rolf Magnus wrote:
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference".


A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )


I use it myself sometimes, because it's a bit shorter than "reference to
const".
The above is a reference to "A* const"


In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.


And what about this case:

A *p;
A * const * q = &p;

p looks like an A * const when viewed through the dereferenced q. Still I
wouldn't call p a const pointer, since it isn't const. It's a pointer to
const, even though the pointed-to object is actually not a constant.

Jul 23 '05 #7

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

Similar topics

1
9988
by: Kent | last post by:
Hi! I am working with an obscure graphics library in school as a university C++ programming project. The graphics library has a class kalled Bitmap wich you use to draw graphics (you can "stamp it down" on the screen, double buffering and such is all within the lib). This is how it works now (i think its a bad design): In a class i created to represent "monsters" in a game i have the following
12
2748
by: Kristian Bisgaard Lassen | last post by:
Hi, How do I parameterize a template by a a allocated array of integers, which is declared static and constant, so I can make compile time optimizations depending on the content of the array. The way I have written my code makes g++ complain about it not being static. BTW I am new to the C++ language and also the templates it provides. Here is an example of what I want to do
11
404
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form (lex.literal). A string literal is an *lvalue*; all other literals are *rvalues*. -4- The operator :: followed by an identifier, a qualified-id, or an operator-function-id is a primary-expression. Its type is specified by the
20
2089
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to the constant variable and if I changed the value through the pointer, the constant variable should have been modified. Well, that's what I thought until I wrote the following and run it. #include <iostream> using std::cout; using std::endl;
11
5293
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string> using namespace std;
11
2369
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
3
2721
by: ramasubramanian.rahul | last post by:
i was reading soemthing about Lvalues Rvalues and modifiable Lvalues.... i am confused now... like for eg int *y , x ; y = &x ; in the second is x a lvalue or rvalue any pointers on some good reading on lvalues and rvalues on the web ?? thanks in advance Kind Regard
12
1291
by: Bob Altman | last post by:
Hi all, I want to call a function that takes a void* argument and pass it the address of a constant. Is there a way to do this without explicitly defining the constant and then passing the address of the constant? In other words, what I *don't* want to do is this: const int myInt = 5; MyFunc(&myInt);
6
1782
by: Ruben | last post by:
re-reviewing the chapter in Lippan and Lajoie C++ Primer the have a section on references that I don't understand and just makes e shake my head believing that any behavior is possible with references and that they can never be understood. Maybe someone else can rephrase this so that I understand the defined behavior. cont int ival = 1024; //error: requires a const reference int *&pr_ref = &ival; //<<== this seems obvious as a...
0
8837
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, 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...
0
9366
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
9214
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...
0
9059
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
8005
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...
1
6668
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 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...
0
4751
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3187
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
3
2129
bsmnconsultancy
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...

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.