Re: explicit call of constructor and destructor
On Dec 5, 11:12 am, "Alf P. Steinbach" <al...@start.no> wrote:
* Abhishek Padmanabh:
On Dec 4, 8:30 pm, James Kanze <james.ka...@gmail.com> wrote:
On Dec 4, 4:47 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
[...]
Ok, thanks for that. So then, how is an operator new overload (which
can be called an allocation function, right?) different from a user
defined placement new form (not using the one for in place
construction as available in <new>)?
"user defined placement form" is AFAIK not a term employed by the
standard, nor is it a commonly used term. But assuming you mean it as a
description: a "new" expression for single object accepts two lists of
arguments,
new (arglist1) T(arglist2)
arglist1 is passed to the allocation function, so by definining an
allocation function (which unfortunately is named "operator new") you
enable a particular signature for this argument list.
arglist2 is passed to a T constructor, so by definining a constructor
you enable a particular signature for this argument list.
There's not really more to it.
Yes, this is okay. Let's go a little further and see if I got it
right.
operator new/operator delete overloads should match the signature of
their global forms provided by the compiler.
No.
And then when the objects
of the class, for which these are overloaded, are created using new
expression, use the operator new overload for allocation and the
corresponding operator delete for deallocation.
Huh?
What I am saying is - if the overloaded operator new has the same
signature as that of the global operator new, then I can create
objects as below:
struct A {
A(/*constructor arg list*/);
void* operator new (size_t size); //same signature
as ::operator new() //1
};
int main()
{
A * ptr = new A(/*constructor arg list*/);
}
If the argument list is changed by adding additional arguments to the
operator new overload for class A as below:
struct A {
A(/*constructor arg list*/);
void* operator new (size_t size, size_t additional_argument); //
different signature as ::operator new() //2
};
int main()
{
A * ptr = new A(/*constructor arg list*/);
}
then the above way of create object of A will fail as a match is not
found but if the statement:
A * ptr = new A(/*constructor arg list*/);
is modified to :
size_t additional_argument=0;
A * ptr = new(additional_argument) A(/*constructor arg list*/);
then the overloaded one will be called and this form of new expression
(which specifies arguments to allocation function) is called a
placement new form. Is that correct?
Also, one such overload is provided for the global allocation function
under the header <new> that does not do any allocation but just does
in place construction. Is that correct?
If placement allocation function and placement deallocation functions
also manage allocation and deallocation then what is different between
these and the above overloads? Just that if the signature if a direct
match with operator new/operator delete, they are overloads else they
are placement allocation functions?
It seems you are assuming a distinction that does not exist.
Since there is no distinction, placement new is any overload of
operator new that gets its additional arguments from the new
expression argument list. Is that correct? So, in the code I posted
above,
//1 - is an overload of operator new
//2 - is an overload of operator new but has additional arguments
and hence relates to placement new form where additional arguments
need to provided as part of arglist1 as in new (arglist1) T(arglist2)
as posted by you.
Also, referring to the example as in 5.3.4 (20) as below:
struct S {
// Placement allocation function:
static void* operator new(std::size_t, std::size_t);
// Usual (non-placement) deallocation function:
static void operator delete(void*, std::size_t);
};
S* p = new (0) S; // ill-formed: non-placement deallocation function=
matches
// placement allocation function
The current standard's =A75.3.4/20 does not have the above example, or any=
example. However, this example is in =A75.3.4/20 of the draft for C++0x.
That paragraph correspond to =A75.3.4/19 in the current standard.
The rule concerning the ill-formed'ness of the above example was added
in the draft.
It's a special case that evidently wasn't considered in the 98 standard.
The standard also says (in 18.5.1.3 - Placement forms)
In the current standard this is =A718.4.1.3.
that the
placement form function is reserved and a C++ program may not define
them that would displace these? Is that specific to the form as below:
void* operator new(std::size_t size , void* ptr ) throw();
No, it's specific to the two forms of operator new and two forms of
operator delete specified there, in the global namespace.
Is this the one that does in-place construction?