If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a function pointer to the new
operator itself.
Asked
Active
Viewed 1,321 times
5

Catskul
- 17,916
- 15
- 84
- 113
-
4`pass a pointer to the new operator itself`, What?!? – karlphillip Jul 07 '11 at 01:39
-
New is an operator that behaves like a function that takes returns a pointer the object you are creating. Conceptually (if not actually) it should be possible to create a function pointer to it, and pass it around as an argument to a function, for example. – Catskul Jul 07 '11 at 01:42
-
1Just as you think you would: `void* (*np) (size_t) = &::operator new;`. Is it just me or are the questions getting more and more Byzantine as time goes by? Bonus question: what happens when you `delete np;`? – Kerrek SB Jul 07 '11 at 01:56
-
@Kerrek, hm interesting. I suppose I should have said a function's new operator. – Catskul Jul 07 '11 at 02:05
2 Answers
9
boost::lambda provides function wrappers for new
and delete
. These can be used to easily convert an new
call into a function object.

bdonlan
- 224,562
- 31
- 268
- 324
2
operator new
(as well as the other flavours) takes care of allocating memory but does not construct objects. In fact its return type is void*
. What constructs an object is a new expression, which is part of the language and not a function. So it's not possible to form a pointer or reference to it; it's as meaningless as forming a reference to return
.

Luc Danton
- 34,649
- 6
- 70
- 114
-
Perhaps not possible, but certainly not as meaningless as forming a reference to `return`. – Catskul Jul 07 '11 at 01:57
-
Well, typically you'd use your custom new-pointer to allocate and then pass the result into a placement-new expression... that isn't entirely unheard of. – Kerrek SB Jul 07 '11 at 01:59
-
@Catskul You can't form a reference to a function to what is not a function. That's what is meaningless. – Luc Danton Jul 07 '11 at 02:02
-
@Kerrek Notice that it's still a new expression constructing the object. And you can't form a reference to it because it's not a function. – Luc Danton Jul 07 '11 at 02:02
-
@Luc: yes, of course, you cannot make a reference to the process of constructing an object. I'm not really sure what the OP is trying to achieve. – Kerrek SB Jul 07 '11 at 02:06
-
@Luc I don't disagree, but conceptually it has some sense. There are effectively functions being called behind the scene, malloc and then of course the constructor. – Catskul Jul 07 '11 at 02:08
-
@Catskul You can say that of almost every expression possible: eventually, in most programs, functions end up being called. But let me clarify; when I say it's *meaningless*, I mean it's meaningless **according to the rules of the language**. For instance, `std::vector<42>` is meaningless. It was not meant as a comment on what you're trying to achieve, or yourself. – Luc Danton Jul 07 '11 at 02:11