A
Alf P. Steinbach
One main usage of references is for arguments, to replace e.g. void foo(int*)
with void foo(int&).
However, our Holy Standard claims that a reference must be initialized with an
_object_, before going on to show examples of initializing with lvalues.
And there are some lvalues -- I cannot find any restrictions on dereferencing
that would forbid this -- that aren't very real objects. They're more like
hypothetical objects. So, is the program below a correct C++ program?
#include <cstddef>
void doStuff( int& firstElem, std::size_t size )
{
// whatever
for( std::size_t i = 0; i < size; ++i ) { (&firstElem); }
}
int main()
{
int grumblegrumble[666];
doStuff( *(new int[0]), 0 );
doStuff( grumblegrumble[666], 0 );
//doStuff( destroyedButNotDeallocated, 0 );
}
The reason I ask is that if it is a correct program, then in a small piece I'm
writing I'll use some special term to denote pointers that do point to fully
real objects, and corresponding references. I thought "RealGood" would be
nice for that purpose, with Un-RealGood, "URG", denoting not RealGood, where
RealGood implies valid. But if the actual arguments in this program are not
allowed by the standard, then the not-quite-objects do not, presumably, even
exist as "objects", and then it would perhaps be incorrect even to dereference
the URG pointers shown here?
with void foo(int&).
However, our Holy Standard claims that a reference must be initialized with an
_object_, before going on to show examples of initializing with lvalues.
And there are some lvalues -- I cannot find any restrictions on dereferencing
that would forbid this -- that aren't very real objects. They're more like
hypothetical objects. So, is the program below a correct C++ program?
#include <cstddef>
void doStuff( int& firstElem, std::size_t size )
{
// whatever
for( std::size_t i = 0; i < size; ++i ) { (&firstElem); }
}
int main()
{
int grumblegrumble[666];
doStuff( *(new int[0]), 0 );
doStuff( grumblegrumble[666], 0 );
//doStuff( destroyedButNotDeallocated, 0 );
}
The reason I ask is that if it is a correct program, then in a small piece I'm
writing I'll use some special term to denote pointers that do point to fully
real objects, and corresponding references. I thought "RealGood" would be
nice for that purpose, with Un-RealGood, "URG", denoting not RealGood, where
RealGood implies valid. But if the actual arguments in this program are not
allowed by the standard, then the not-quite-objects do not, presumably, even
exist as "objects", and then it would perhaps be incorrect even to dereference
the URG pointers shown here?