Clarification in Thinking in C++ Vol 1

G

gopal

Hi, i am now reading thru the thinking in C++ and i have some doubts at
section CONST REFERENCES as


The use of const references in function arguments is especially
important because your function may receive a temporary object.
This might have been created as a return value of another function
or explicitly by the user of your function. Temporary objects are
always const, so if you don't use a const reference, that argument
won't be accepted by the compiler. As a very simple example,


page - 498 , chapter 11, Ref & Copy ctor,

can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand

//: C11:ConstReferenceArguments.cpp
// Passing references as const
478 Thinking in C++ www.BruceEckel.com
void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error
g(1);
} ///:~


Pls clarify

Regards
JK
 
F

Frederick Gotham

gopal posted:

Temporary objects are always const


That's incorrect.


class ArbitraryClass {
public:

int data;

void NonConstMethod()
{
data = 44;
}

};


int main()
{
ArbitraryClass().NonConstMethod();
}


can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand
void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error


There's only one kind of thing which you can bind a non-const reference to,
and that's a non-const l-value.


Const references, on the other hand, are far more flexible, and you can
bind them to anything, even an R-value:


const int& i = 56;
 
V

Victor Bazarov

gopal said:
Hi, i am now reading thru the thinking in C++ and i have some doubts

"Doubts"? About what?
at section CONST REFERENCES as


The use of const references in function arguments is especially
important because your function may receive a temporary object.

A non-const reference cannot be bound to a temporary. That's a C++
language requirement.
This might have been created as a return value of another function
or explicitly by the user of your function. Temporary objects are
always const,

That is simply not true. Disallowing of binding non-const references
to temporaries has *nothing* to do with possible constness of the
temporary object. Besides, in most cases temporaries are *not* at all
constant.
so if you don't use a const reference, that argument
won't be accepted by the compiler. As a very simple example,


page - 498 , chapter 11, Ref & Copy ctor,

can any pls make me understand this. I tried reading thru again &
again, but i am not able to understand. This was foll by example which
i could not understand

What exactly *could not* you understand?
//: C11:ConstReferenceArguments.cpp
// Passing references as const
478 Thinking in C++ www.BruceEckel.com
void f(int&) {}
void g(const int&) {}
int main() {
//! f(1); // Error
g(1);
} ///:~


Pls clarify

In 'f(1)' expression the temporary behind the numeric literal 1, which
is created at some point before calling the function and is destroyed
right after full expression evaluation, cannot be bound to a reference
to non-const 'int' as calling 'f' would require. That's why the compiler
reports an error here.

The explanation is a bit more convoluted than that temporaries are const.
They are not. The reason is that during binding a temporary to a reference
another temporary can be created if needed for proper type matching. For
example:

void f(double& d);
foid g(double const& d);

f(1); // '1' is integer
g(1); // '1' is integer

Now, if 'f' wanted to try to change its argument, and a temporary of type
'double' were created, then the change would never be propagated to the
temporary associated with it, since there would be another, intervening,
temporary. That's why it was decided to only allow references to const
to bind to a temporary.

V
 
F

Frederick Gotham

Victor Bazarov posted:
That's why it was decided to only allow
references to const to bind to a temporary.


You probably meant something like:

It was decided to only allow references to non-const to bind to non-const
L-values.
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:



You probably meant something like:

It was decided to only allow references to non-const to bind to
non-const L-values.

Whatever.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top