array of reference?

R

Rahul

why can't we have array of references.

Reference is just an alias to the variable, no memory is allocated
for a reference. In fact, we can't have a reference to a reference or
a pointer to a reference. Hope it clarifies.
 
T

terminator

* siddhu:


A reference is not an object, it has no size.

it has a size usually equal to that of a pointer in case of usage an
argument to a none inlined function.
 
V

Victor Bazarov

terminator said:
it has a size usually equal to that of a pointer in case of usage an
argument to a none inlined function.

And when the size isn't equal to that of a pointer, what is it?

V
 
A

Alf P. Steinbach

* terminator:
it has a size usually equal to that of a pointer in case of usage an
argument to a none inlined function.

Sorry, that's incorrect. Or not even wrong. If you want to make an
argument, cough up chapter and verse from the standard.

- Alf
 
J

Joe Greer

it has a size usually equal to that of a pointer in case of usage an
argument to a none inlined function.

No, references don't even have to have a physical representation.
Analogous to typedef, a reference only introduces a new name for an object.
It turns out that usually memory for an address is allocated for reference
parameters and reference object members, but it would be a pretty poor
compiler that allocated anything at all for a local reference.

joe
 
A

Abhishek Padmanabh

* siddhu:


A reference is not an object, it has no size.

If by size you mean storage, it is *unspecified* that they have
storage or not. So, saying they have no size would be incorrect.
Chapter and verse: 8.3.2 (4) (2007-06-25 draft). :)
 
V

Victor Bazarov

Abhishek said:
If by size you mean storage, it is *unspecified* that they have
storage or not. So, saying they have no size would be incorrect.
Chapter and verse: 8.3.2 (4) (2007-06-25 draft). :)

The more proper way of answering the original question would then
be "becuase the Standard says so in [dcl.ref]/4".

However, if it's unspecified whether they have storage or not, it
usually means that in a general case they do NOT have storage, and
hence they do NOT have size.

V
 
A

Abhishek Padmanabh

why can't we have array of references.

Arrays of references are not possible because they may or may not
require storage (implemented as to have size or not). In the cases
(implementations) when they won't have a size, arrays of them won't be
possible. References are not considered as objects by the standards.
They are just aliases. In next standards revision, there might be a
reference_wrapper added that can be used instead of plain references
wherever you need to make arrays of them or use them with standard
containers.
 
A

Abhishek Padmanabh

If by size you mean storage, it is *unspecified* that they have
storage or not. So, saying they have no size would be incorrect.
Chapter and verse: 8.3.2 (4) (2007-06-25 draft). :)

The more proper way of answering the original question would then
be "becuase the Standard says so in [dcl.ref]/4".

Yes, sorry about that. Having a later draft caused some confusions
earlier. I now note that quoting the section is a better option to
avoid mismatches between the numbering/order. Thanks for the tip.

Off-topic, but I recently bumped into std::accumulate which the C++98
and probably C++03 mention as to not have any side-effects but this
version had more explicit explaination that it would not change the
objects pointed to by the iterators, invalidate them etc. And not
knowing that we just got confused (got revealed later, though).
 
T

terminator

No, references don't even have to have a physical representation.
Analogous to typedef, a reference only introduces a new name for an object.
It turns out that usually memory for an address is allocated for reference
parameters and reference object members, but it would be a pretty poor
compiler that allocated anything at all for a local reference.

joe

I said in case of usage as **an argument to a none inlined function**.
 
T

terminator

* terminator:



Sorry, that's incorrect. Or not even wrong. If you want to make an
argument, cough up chapter and verse from the standard.

I do not get it:

void f(int& iref);

can you explain how 'iref ' is passed to 'f' if it is not inlined?
The simplest way is to implement it as a pointer ,so stack register
will be decreased sizeof(int*) for passing 'iref'.

regards,
FM.
 
T

terminator

And when the size isn't equal to that of a pointer, what is it?

Nothing,A pseudo C++ instruction, not even some gaseus.when not an
argument to out of line functions it is resolved at compile time.

regards,
FM.
 
J

Joe Greer

I said in case of usage as **an argument to a none inlined function**.

Sadly (or is it gladly? Depends upon the topic and POV.) you don't
really have any control over that. Some of today's modern compilers can
inline in the linker stage. Some classes with reference member
variables can also be unrolled and inlined during compilation.
Therefore, it's hard to make any flat statement about the storage of
references other than they don't require any.

joe
 
A

Alf P. Steinbach

* Abhishek Padmanabh:
If by size you mean storage, it is *unspecified* that they have
storage or not. So, saying they have no size would be incorrect.
Chapter and verse: 8.3.2 (4) (2007-06-25 draft). :)

A reference has no size. Applying sizeof to reference yields the size
of the referent, not the reference itself. Within the standard language
there's no way to find the size of the storage, if any, that a reference
occupies.

Cheers, & hth.,

- Alf
 
A

Alf P. Steinbach

* Abhishek Padmanabh:
Arrays of references are not possible because they may or may not
require storage (implemented as to have size or not). In the cases
(implementations) when they won't have a size, arrays of them won't be
possible. References are not considered as objects by the standards.
They are just aliases. In next standards revision, there might be a
reference_wrapper added that can be used instead of plain references
wherever you need to make arrays of them or use them with standard
containers.

References simply don't have sizes.

For practical programming, not waiting until C++0x, There is a reference
wrapper in Boost, IIRC.

Cheers, & hth.,

- Alf
 
J

Juha Nieminen

Alf said:
Sorry, that's incorrect. Or not even wrong. If you want to make an
argument, cough up chapter and verse from the standard.

He said "usually", which I understand to mean "in practice with
current compilers and architectures".
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top