Bo Persson wrote:
You cannot call a constructor directly, because it doesn't have a
name.
I have read this argument several times. However, I do not understand the
inference: I agree that constructors do not have names. But where in the
standard do you find the other hypothesis needed for the inference, namely
that you need a name to call someting? This additional assumption might be
something that you consider natural because functions are called by using
their names. However, constructor calls do not need to be parallel to
function calls. In my view, it is perfectly natural to regard placement new
as a special syntax provided by the standard for calling constructors.
This syntax does not require constructors to have names, since the class
name is used instead. The compiler then deduces for you which constructors
is to be used (somewhat like it deduces which function you meant to call in
the case of overloaded functions). So again: where in the standard do you
find the provision that something needs to have a name for us to be able to
call it?
Most attempts to "call a constructor" will create a temporary of
the class type instead.
class C {};
C::C(); // creates a temporary of class C
And how does that not call a constructor? If I am not mistaken, an
expression of the form classname( arguments ) is even named an "explicit
constructor call" in the standard.
In practice, you invoke the constructor as part of executing a new
expression, like placement new.
So I can invoke the constructor but I cannot call it? Where in the standard
would I find the distinction between "to invoke" and "to call". As far as I
can see, the two words are both used synonymously in the standard with
regard to constructor calls.
Best
Kai-Uwe Bux