Topic: Interpretation/Implementation of C++ References
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/07/15 Raw View
j.sampaio@ip.pt wrote:
>
> Steve Clamage wrote:
>
> > I read section 3.7.3 and its subsections to say that any function
> > called "operator new" or "operator new[]" must have a return type
> > of void*.
>
> Hi,
> I am in no way questioning anything that has been said, but, please read the
> following that is in Marshal Kline's FAQ (of comp.lang.c++):
>
> [16.3]
> [...] Type Safety: malloc() returns a void* which isn't type safe. new
> Fred() returns a pointer of the right type (a Fred*).
>
> and, in fact, when I write
>
> class b {};
> class c {};
> //....
> c* ptc=new b;
>
> the compiler marks it has an error.
> What am I not understanding?
What you've missed is the distinction between a new-expression and
operator new(). 'new b' is an new-expression. It does two main things -
it calls the appropriate operator new() to allocate the memory, and then
calls the appropriate constructor to initialize that memory. operator
new() returns void *; a new-expression returns a pointer to the
specified type.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/07/12 Raw View
j.sampaio@ip.pt writes:
>Steve Clamage wrote:
>> I read section 3.7.3 and its subsections to say that any function
>> called "operator new" or "operator new[]" must have a return type
>> of void*.
>I am in no way questioning anything that has been said, but, please read the
>following that is in Marshal Kline's FAQ (of comp.lang.c++):
>[16.3]
>[...] Type Safety: malloc() returns a void* which isn't type safe. new
>Fred() returns a pointer of the right type (a Fred*).
That's the difference between a new-expression, as in
T* p = new T;
and an allocation function whose name is operator new, such as
void* operator new(size_t) throw (std::badalloc);
A new-expression has two effects: It calls an operator new to
allocate memory, and if that succeeds, invokes a constructor
for the type of object being created. (It has some other
exception-related properties as well.) The result of the
new-expression is a pointer of the specified type.
This discussion concerns allocation functions, not new-expressions.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/07/12 Raw View
clamage@eng.sun.com (Steve Clamage) writes:
> I read section 3.7.3 and its subsections to say that any function
> called "operator new" or "operator new[]" must have a return type
> of void*.
Yes, I agree with you. My bad.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: j.sampaio@ip.pt
Date: 1999/07/12 Raw View
Steve Clamage wrote:
> I read section 3.7.3 and its subsections to say that any function
> called "operator new" or "operator new[]" must have a return type
> of void*.
Hi,
I am in no way questioning anything that has been said, but, please read the
following that is in Marshal Kline's FAQ (of comp.lang.c++):
[16.3]
[...] Type Safety: malloc() returns a void* which isn't type safe. new
Fred() returns a pointer of the right type (a Fred*).
and, in fact, when I write
class b {};
class c {};
//....
c* ptc=new b;
the compiler marks it has an error.
What am I not understanding?
Thanks,
Joao
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Varghese 'sam Samuel" <samvarghese@yahoo.com>
Date: 1999/07/08 Raw View
C++ Aliasing/References
------------------------
Q.1.
Regarding interpretation of the C++ standard on references; does it
imply that C++ references must be implemented by pointers?
Or are we talking about it only in that for all practical purposes
language implementors implement references using pointers but it is
possible for other types of implementation ??!!
Also, what does this mean it terms of aliasing properties??
Q 2.
Since in the C++ language, the 'new' operator always returns a
pointer;is it within the bounds of a standard C++ libary to write a
specialised library version of the new<> operator such that it returns
a reference?
Sam
Thought for the day.....:-)
'The overloading of the * symbol has been the curse of the C/C++
language'
Varghese 'Sam' Samuel
"Sam Consulting"
--
Varghese 'Sam Samuel
"Sam Consulting"
5101 River road
#1907
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/07/08 Raw View
"Varghese 'sam Samuel" <samvarghese@yahoo.com> writes:
>C++ Aliasing/References
>------------------------
>Q.1.
>Regarding interpretation of the C++ standard on references; does it
>imply that C++ references must be implemented by pointers?
No. References have certain properties. They can be implemented
by any method that satisfies those properties.
>Or are we talking about it only in that for all practical purposes
>language implementors implement references using pointers
Yes. It is an obvious and efficient way to meet all requirements
on common computer architectures.
> ... but it is possible for other types of implementation ??!!
In principle, yes. I've never heard of any other implementation
method, however.
>Also, what does this mean it terms of aliasing properties??
A reference is an alias, by definition. After creating the
reference, any mention of the reference really refers to the
thing that it aliases.
>Q 2.
>Since in the C++ language, the 'new' operator always returns a
>pointer;is it within the bounds of a standard C++ libary to write a
>specialised library version of the new<> operator such that it returns
>a reference?
No. There are two requirements in particular on every operator new:
1. It must have a return type of void*.
2. It must have a first parameter of type size_t.
You can write your own allocation function that works any way
you want; you just can't give it the name "operator new" unless
it meets all the requirements for operator new.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/07/09 Raw View
"Varghese 'sam Samuel" <samvarghese@yahoo.com> writes:
> Regarding interpretation of the C++ standard on references; does it
> imply that C++ references must be implemented by pointers?
No.
> Or are we talking about it only in that for all practical purposes
> language implementors implement references using pointers but it is
> possible for other types of implementation ??!!
Other types are possible, including nothing at all if the compiler
can determine that a reference always refers to some particular
object that is otherwise accessible. For example, in the following
struct self { self &me; self() : me(*this) { } };
the compiler can leave out any direct representation of 'me' at all.
> Also, what does this mean it terms of aliasing properties??
I don't understand this question.
> Since in the C++ language, the 'new' operator always returns a
> pointer;is it within the bounds of a standard C++ libary to write a
> specialised library version of the new<> operator such that it returns
> a reference?
I believe that you are permitted to write operator new functions that
return odd types, as long as the parameters don't match one of the
required operator new functions. It would be an error if you wrote a
new expression that caused such an operator new to be invoked; you
would have to call operator new explicitly. You could just write a
set of template functions to do the dereferencing, eg.,
template <typename T> T &newref() { return *new T(); }
myClass &joe = newref<myClass>();
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/07/10 Raw View
Varghese 'sam Samuel wrote:
>
> C++ Aliasing/References
> ------------------------
> Q.1.
> Regarding interpretation of the C++ standard on references; does it
> imply that C++ references must be implemented by pointers?
No.
>
> Or are we talking about it only in that for all practical purposes
> language implementors implement references using pointers but it is
> possible for other types of implementation ??!!
Yes. An obvious case would be if pointers are not implemented
as just addresses (f.ex. "fat pointers" for bounds checking),
references could still be implemented as simple address (since
they don't have pointer arithmetic, they don't need the related
debugging info).
In fact, the only reason that references and pointers are
generally implemneted the same way is the fact that the simplest
implementation for each one is the same: the memory address
of the pointed to/referenced object. This is information
that is needed in any case, and it's enough information to
provide the required functionality.
[...]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/07/10 Raw View
Hyman Rosen <hymie@prolifics.com> writes:
>"Varghese 'sam Samuel" <samvarghese@yahoo.com> writes:
>> Since in the C++ language, the 'new' operator always returns a
>> pointer;is it within the bounds of a standard C++ libary to write a
>> specialised library version of the new<> operator such that it returns
>> a reference?
>I believe that you are permitted to write operator new functions that
>return odd types, as long as the parameters don't match one of the
>required operator new functions.
I read section 3.7.3 and its subsections to say that any function
called "operator new" or "operator new[]" must have a return type
of void*.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]