Re: BSD Unix: Weird call to structure constructor in formatter.h
* Gerhard Menzl:
Alf P. Steinbach wrote:
>> Sorry, there are expressions that look like explicit constructor calls
and really are: they're constructor calls, and they're explicit. This
has already been discussed, but perhaps a new round of discussion is
needed for each person. And on the other hand, placement new, which
looks like and which you state above has the effect of a direct
constructor call (note: difference between explicit and direct), is a
very indirect constructor call. In particular, it's unspecified
whether the constructor arguments are evaluated before or after the
allocation function is called (and that function can throw). Without
understanding the indirectness of new and placement new, this can be
hard to grok.
Which allocation function?
Ah, to understand that, even more terminology must be straightened out...
Terminology: ?5.3.4/2 "The new-placement syntax is used to supply
additional arguments to an allocation function".
Elsewhere in the standard that syntax is called "placement new" (an
informal readable phrase) instead of "new-placement" (a formal name for
a part of the grammar). For example, ?3.2/2 "allocation function for
placement new".
I am talking about an expression along the lines of
new (p) A(x, y, z);
This is an example of using the placement new syntax.
It is the most basic form.
The more general syntax is what "placement new" refers to in general
(see references to the standard above).
given the standard placement new operator
void* operator new(size_t, void* p) { return p; }
This is an allocation function (AFAIK there's no such thing as
"placement new operator", but just "new operator" would be a descriptive
term), see ?3.7.3, and in particular ?3.7.3/1 "Allocation functions".
There are an infinite number of possible allocation functions that can
be invoked via placement new.
Depending on the definition of A above, that expression may invoke the
standard do-nothing allocation function, or some other allocation
function. To ensure that the standard do-nothing allocation function is
used, the form exemplified by '::new (p) A(x, y, z);' can be employed.
The <new> header must then be included.
This is as close to a naked constructor invocation as you can get, but
it does not look like an ordinary function call.
Use of the standard do-nothing allocation function is in a sense close
to, or simply is, a "naked" constructor call, yes, I agree, because
there's no allocation. Another example is 'A x;' at namespace scope,
where there's no allocation at run time. And depending on one's view,
very formal or very practical, the same could be said about an automatic
variable. But I don't think it's a very important or even practical
distinction: it's the first time I've seen this composite term.
[rest snipped, agreement]
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]