C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

q: address of constructor (and destructor)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
root
Guest





PostPosted: Sat Aug 09, 2003 9:31 pm    Post subject: q: address of constructor (and destructor) Reply with quote



Why does c++ not allow taking an address of a constructor or destructor?

I know people say it is useless but if the language wanted to support
it, would it be technically possible? If so, how? If not why?

What I got so far based on the answers from comp.lang.c++ is that
- it is useless AND
- to support it, it needs a different mechanism from ptr to normal
member functions

Hence, it is NOT allowed.

If this is true, then what I want to know is why, to support it, we
needs a different mechanism from ptr to normal member functions.

thanks
kn

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Edward Diener
Guest





PostPosted: Sun Aug 10, 2003 12:00 am    Post subject: Re: address of constructor (and destructor) Reply with quote



root wrote:
Quote:
Why does c++ not allow taking an address of a constructor or
destructor?

I know people say it is useless but if the language wanted to support
it, would it be technically possible? If so, how? If not why?

If there is no use for it in the language, why would you want it supported ?
You must prove a use, else adding features to the language with no good use
at all is just complicating the language unnecessarily.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
James Dennett
Guest





PostPosted: Sun Aug 10, 2003 12:21 am    Post subject: Re: q: address of constructor (and destructor) Reply with quote



root wrote:
Quote:
Why does c++ not allow taking an address of a constructor or destructor?

Because they're not regular functions, and there is
no syntactical way to take their address. Syntax could
be added, but it's not worth doing so unless someone
makes a case for it. I don't think a successful case
is likely to be made.

Quote:
I know people say it is useless but if the language wanted to support
it, would it be technically possible? If so, how? If not why?

No need.

template <typename TypeToConstruct>
void call_constructor(void *address)
{
(void) new (address) TypeToConstruct;
}

You can take the address of call_constructor<T> if you
wish.

Quote:
What I got so far based on the answers from comp.lang.c++ is that
- it is useless AND

Fair comment.

Quote:
- to support it, it needs a different mechanism from ptr to normal
member functions

Regular function pointers are sufficient, no need for ptmfs.
A constructor does not act on an existing object, it acts on
raw memory and converts it to an object.

-- James.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
root
Guest





PostPosted: Sun Aug 10, 2003 8:33 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote


Quote:

So what would the type of a ctor be? AFAICT every ctor has a unique type
which sort of makes its address pointless:)



Assuming ptr to member function was to use for ctor also:



class apple {
int _i;
public:
explicit apple():_i(Cool {}

void f(int i) {....}
};

typedef void(apple::*PMF)(int);

PMF pmf1 = &apple::apple; //take int arg for addr to initialize
PMF pmf2 = &apple::f;

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
root
Guest





PostPosted: Sun Aug 10, 2003 9:16 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote


Quote:

Because they're not regular functions, and there is
no syntactical way to take their address. Syntax could
be added,



This sounds like the kind of reasoning I'm looking for. BUt can you
elaborate more on the part
"they're not regular functions, and there is
no syntactical way to take their address"

You say "....Syntax could be added", can u give me some example please.

Quote:

- to support it, it needs a different mechanism from ptr to normal
member functions


Regular function pointers are sufficient, no need for ptmfs.
A constructor does not act on an existing object, it acts on
raw memory and converts it to an object.

The above you say ".... there is
no syntactical way to take their address." AND here you say normal
ptr is sufficient.


What am I misunderstanding?

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Francis Glassborow
Guest





PostPosted: Sun Aug 10, 2003 9:21 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote

In article <bJOdna8zU511PquiU-KYuQ (AT) comcast (DOT) com>, root
<royalmacaronipenguin (AT) yahoo (DOT) com> writes
Quote:

So what would the type of a ctor be? AFAICT every ctor has a unique
type which sort of makes its address pointless:)


Assuming ptr to member function was to use for ctor also:



class apple {
int _i;
public:
explicit apple():_i(Cool {}

void f(int i) {....}
};

typedef void(apple::*PMF)(int);

But that is wrong. ctors do not have return types FULL STOP and it would
be an error to make it otherwise, they are fundamentally different form
member functions that return void.

Quote:

PMF pmf1 = &apple::apple; //take int arg for addr to initialize
PMF pmf2 = &apple::f;


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
root
Guest





PostPosted: Mon Aug 11, 2003 2:05 am    Post subject: Re: q: address of constructor (and destructor) Reply with quote

Francis Glassborow wrote:
Quote:
In article <bJOdna8zU511PquiU-KYuQ (AT) comcast (DOT) com>, root
[email]royalmacaronipenguin (AT) yahoo (DOT) com[/email]> writes


So what would the type of a ctor be? AFAICT every ctor has a unique
type which sort of makes its address pointless:)


Assuming ptr to member function was to use for ctor also:



class apple {
int _i;
public:
explicit apple():_i(Cool {}

void f(int i) {....}
};

typedef void(apple::*PMF)(int);


But that is wrong. ctors do not have return types FULL STOP and it would
be an error to make it otherwise, they are fundamentally different form
member functions that return void.


yeah I agree. I did hat because my compiler happens to internally define

a constructor that way. And yes I also agree that it's not the standard.

About the pointless part. I agree then.
But that doesn't answer my question "why can't we take the address of a
cotr or dtor?" Again,

According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter 15 Class Hierarchies, Section 5 Free Store,
Subsection 2 "Virtual Constructors", page 424:

"Furthermore, a constructor is not quite an ordinary function.
In particular, it interacts with memory management routines
in ways that ordinary member functions don't.
Consequently, you cannot have a pointer to a constructor."

It doesn't sound like because it is useless that it is not allowed. BUt
can anybody elaborate what he says there?

thanks,
kn



---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Marcin 'Qrczak' Kowalczyk
Guest





PostPosted: Mon Aug 11, 2003 2:05 am    Post subject: Re: q: address of constructor (and destructor) Reply with quote

root wrote:

Quote:
PMF pmf1 = &apple::apple; //take int arg for addr to initialize
PMF pmf2 = &apple::f;

pmf1 and pmf2 would have the same type, but the set of pointers they can
be called on is almost disjoint. You would need to call a constructor on
an uninitialized memory and a method on an initialized object.

IMHO it would be too easy to forget it and call the constructor on an
initialized object, which is conceptually wrong, even if it works for
POD types.

--
__("< Marcin Kowalczyk
__/ [email]qrczak (AT) knm (DOT) org.pl[/email]
^^ http://qrnik.knm.org.pl/~qrczak/

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
root
Guest





PostPosted: Mon Aug 11, 2003 4:28 am    Post subject: Re: q: address of constructor (and destructor) Reply with quote

Marcin 'Qrczak' Kowalczyk wrote:
Quote:
root wrote:


PMF pmf1 = &apple::apple; //take int arg for addr to initialize
PMF pmf2 = &apple::f;


pmf1 and pmf2 would have the same type, but the set of pointers they can
be called on is almost disjoint. You would need to call a constructor on
an uninitialized memory and a method on an initialized object.

IMHO it would be too easy to forget it and call the constructor on an
initialized object, which is conceptually wrong, even if it works for
POD types.


how about the case of destructor for "placement new" where you can call
it for an object.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
root
Guest





PostPosted: Mon Aug 11, 2003 4:59 am    Post subject: Re: q: address of constructor (and destructor) Reply with quote


Quote:

They are "special" functions. Constructors cannot be called by
users; they are only invoked by the new operator (not to be
confused with operator new).


sure. how about the case of dtor? It can and should be called in case of
the placement new


Quote:
Sure. We could change the language to allow

struct s {};

typedef void (*no_arg_constructor)();
no_arg_constructor constructor_of_s(__constructor<no_arg_constructor>(s));

If we defined a new keyword __constructor.


Can't we just use the normal syntax for a member function since it
already exists:

no_arg_constructor constructor_of_s = &s::s; //if s has a constructor


There is no syntax you can currently use to
Quote:
take the address of a constructor, but if you could do so
then an implementation would not need the complexity of ptmfs
to store the address of a constructor.

yes but my point is that if ptr to member function is sufficient too, we
can just use it and the normal syntax used to take a member function's
address to assign to it like

typedef void(PMF*)();

PMF pmf = &apple::apple;

That is if we do NOT need an additional, special support for it, why
disallow it (both ctor and dtor)?


thanks,
kn

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Richard Smith
Guest





PostPosted: Mon Aug 11, 2003 3:11 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote

root wrote:

Quote:
Francis Glassborow wrote:
root wrote:

typedef void(apple::*PMF)(int);

This effectively takes two arguments: the "this" argument
and an integer. I think it more likely that the type should
be one of

void (apple::*PMF)();

or
void (*PMF)(apple&);

.... probably the latter.


Quote:
But that is wrong. ctors do not have return types FULL STOP and it would
be an error to make it otherwise, they are fundamentally different form
member functions that return void.

I've never really understood why a constructor is
fundamentally any different from a function returning void
(syntactic issues of use aside). In practice the calling
convention used by many compilers for constructors is
exactly the same as ordinary functions returning void.

struct X {
static void __constructor(X& uninitialised_object);
};


Quote:
About the pointless part. I agree then.
But that doesn't answer my question "why can't we take the address of a
cotr or dtor?" Again,

For a start, there's more than one sort of constructor and
destructor.

struct B {
virtual ~B();
};

B* make(); // creates some unknown derived class

int main() {
B* b1 = new D;
b1->~B(); // calls D::~D() -- see below

B* b2 = new D;
delete b2; // calls D::~D() and D::operator delete
}

struct D : B {
void* operator new(std::size_t);
void operator delete(void*);
};

B* make() { return new D; }


Add to this virtual bases (only the most derived class calls
virtual base constructors / destructors) and you have three
sorts of destructor:

1. Calls base class destructors but not virtual base
destructors.

2. Calls base class destructors and virtual base
destructors.

3. Calls base class destructors, virtual base destructors
and operator delete.

If I take a pointer to a destructor, which one should I get?
I expect the best answer is (2), but which ever is chosen
it'll be wrong for someone. If you really need a pointer to
a destructor, why not do something like

template <class T> void destroy(T* t) { t->~T(); }
int main() {
void (*destructor)(my_type*) = &destroy<my_type>;
}

Quote:
"Furthermore, a constructor is not quite an ordinary function.
In particular, it interacts with memory management routines
in ways that ordinary member functions don't.
Consequently, you cannot have a pointer to a constructor."

If the object is being allocated on the stack, someone needs
to make space on the stack for that object (typically this
has to be the caller) and pass a pointer to that memory
location. Similarly, if the object is heap allocated,
someone needs to call operator new, and make sure the
correct operator delete is called if the constructor throws
an exception. (Note that this deletion operator is not in
general the same one that gets called if the constructor
succeeds.)

Quote:
It doesn't sound like because it is useless that it is not allowed. BUt
can anybody elaborate what he says there?

If pointers to constructors and/or destructors were allowed,
it would mean specifying in much more detail exactly how
memory management interacts with constructors and
destructors. The standard currently just states what has to
be done, and is silent on most issues of who has to do them.

As I can't see any reason to want constructor / destructor
pointers, it seems better that the standard should avoid
this extra complexity by forbidding them.

--
Richard Smith

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Andy Sawyer
Guest





PostPosted: Tue Aug 12, 2003 6:40 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote

In article <bJOdna8zU511PquiU-KYuQ (AT) comcast (DOT) com>,
on Sun, 10 Aug 2003 20:33:47 +0000 (UTC),
[email]royalmacaronipenguin (AT) yahoo (DOT) com[/email] (root) wrote:

Quote:
So what would the type of a ctor be? AFAICT every ctor has a unique
type which sort of makes its address pointless:)


Assuming ptr to member function was to use for ctor also:



class apple {
int _i;
public:
explicit apple():_i(Cool {}

void f(int i) {....}
};

typedef void(apple::*PMF)(int);

PMF pmf1 = &apple::apple; //take int arg for addr to initialize

You need an object to bind the pointer to in order to call it. How do
you plan to do that?

Regards,
Andy S
--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Francis Glassborow
Guest





PostPosted: Tue Aug 12, 2003 6:41 pm    Post subject: Re: address of constructor (and destructor) Reply with quote

In article <bJOdna4zU53LOKuiU-KYuQ (AT) comcast (DOT) com>, root
<royalmacaronipenguin (AT) yahoo (DOT) com> writes
Quote:
It doesn't sound like because it is useless that it is not allowed. BUt
can anybody elaborate what he says there?

You have it backwards. We did not sit down and write a special rule to
disallow taking the address of ctors and dtors, we just chose not to
spend time writing a rule that would allow their addresses to be taken.

The grammar of C++ requires that the declaration of a variable start
with a type name -- well just to complicate things we can start with a
type qualifier such as const but we inherited that from C (well actually
C++ invented const but chose to use the same rules as already aplied to
modifiers such as unsigned)

Supposing that we allowed pointers to members to hold the 'address' of a
ctor so we could write how would you declare a variable of that type?
Doing what is suggested in another post and writing

typedef void (apple::*PMF)(int);
PMF pmf1 = &apple::apple;
PMF pmf2 = &apple::f;

does not work because how does the compiler deal with:

void foo(PMF a_ptr_to_mbr){

now how do you use that parameter? if the argument passed is pmf1 the
call syntax and the semantics will be quite different from that for
pmf2.

So that does not work. Now why would we want to add syntax (I am not
going to kill brain cells trying to imagine what that might be) to
handle pointers to ctors when it simply does not buy us anything.

If you think differently produce a coding problem that it would solve.



--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Hyman Rosen
Guest





PostPosted: Tue Aug 12, 2003 6:41 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote

root wrote:
Quote:
Why does c++ not allow taking an address of a constructor or destructor?

Constructors and destructors are very complicated beasts. Because of
multiple and virtual inheritance, and the fact that while a *tor is
running the type of the object is the class of that *tor, invocations
of *tors do a lot of manipulation behind the scenes, and indeed they
may come in several different versions, silently selected depending
on circumstance.

Suppose you had such a pointer. What is your expected usage of it?
You may be able to use templates to get the effect you want:

#include <memory>
template <typename T>
struct cdtor
{
static void destruct(T *p) { p->~T(); }

static T *construct(void *p) { return new(p) T; }

template <typename A1>
static T *construct(void *p, A1 a1) { return new(p) T(a1); }

template <typename A1, typename A2>
static T *construct(void *p, A1 a1, A2 a2) { return new(p) T(a1,a2); }

// etc.
};

struct A { A(int x, double y) : x(x), y(y) { } int x; double y; };

A *(*init)(void *, int, double) = &cdtor<A>::construct<int, double>;

Variations on this theme should come readily to mind.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Matthew Collett
Guest





PostPosted: Tue Aug 12, 2003 6:42 pm    Post subject: Re: q: address of constructor (and destructor) Reply with quote

In article <Ev2E$vBPXrN$Ew$K (AT) robinton (DOT) demon.co.uk>,
[email]francis (AT) robinton (DOT) demon.co.uk[/email] (Francis Glassborow) wrote:

Quote:
But that is wrong. ctors do not have return types FULL STOP and it would
be an error to make it otherwise, they are fundamentally different form
member functions that return void.

But are they 'fundamentally different' from a static member function
returning the constructed type? A pointer-to-constructor would surely
be a pointer-to-function rather than a pointer-to-member-function. (A
pointer-to-destructor OTOH would obviously need to be a
pointer-to-member-function.) Consider:

class Widget {
public:
Widget();
Widget(int);
static Widget defaultWidget() { return Widget();}
};

Widget magicWidget() {return Widget(42);}

int main() {

Widget (*widgetMaker)();

widgetMaker = &magicWidget; //OK.
widgetMaker = &Widget::defaultWidget; //OK.
widgetMaker = &Widget::Widget; //Not OK. Why not?

}

AFAICS, there is no _semantic_ reason not to allow widgetMaker to point
directly to the constructor. The fact that the wrapper function
defaultWidget does absolutely nothing except forward the call makes this
clear. There is however the _syntactic_ problem that the constructor
cannot be named.

Best wishes,
Matthew Collett

--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken. -- Aristotle

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.