constructor

R

Rolf Magnus

Alf said:
* Victor Bazarov:
Alf P. Steinbach said:
[...]
Every tree of constructor calls start with a source code level
constructor call, it's just a very common misconception that they're
never "explicit", or not "calls" (that last misconception is mentioned
in the FAQ, by ref to the definition of default constructor). The
first misconception probably stems from a passage in the standard where
the word "explicit" is used for disambiguation in a particular context,
and the last misconception probably stems from the desire to teach
newbies not to think of a constructor as a an ordinary callable member
function, but doing that by inventing a simple and incorrect
explanation instead of how things actually work.

12.1/2 "Because the constructors do not have names, they are never found
during name lookup". Yes, you can _cause_ a constructor to be called.

That's correct.
Yes.
You can never _call_ one, though.

That's incorrect.

No. Let's see how a C++ programmer calls a function. I see two ways:

1. Write function's name, followed by '(', a parameter list and ')'.
2. Through a pointer, which is similar, except that instead of the
function's name, you use a pointer to it. However, to make a pointer point
to it, you need to have the function's name too.

We could get a bit deeper by adding member function syntax and stuff like
that, but that isn't really significant here. The important part is that
both ways require you to have the function's name. Now let's look again at
the standard quote from above. It clearly says "constructors do not have
names", so you cannot call a constructor. It's that simple.
That, however doesn't mean that constructors are never called. If they
weren't, why would they exist? If you have a function:

void foo()
{
bar();
baz();
}

and you call it like:

int main()
{
foo();
}

then you can say that you called foo() from main(). You cannot say you
called bar() or baz() from main, but they still get called - as a result of
foo() being called. The same is true for constructors. If you write:

#include <iostream>

struct X
{
X() { std::cout << "Hello, world\n"; }
};

int main()
{
X x;
}

again, you didn't call a constructor from main(). You created an object. And
as a result of that object creation, the constructor is called.
There is - however - a difference to the example before: While you could
call bar() and baz() yourself (i.e. directly from main()), you cannot call
a constructor yourself (i.e. directly from main()). It will be called for
you automatically as part of the object creation.
I seem to recall we have discussed that before, and so I simply direct
you again to the same place (definition of default constructor), "can be
called".

Yes, of course it can be called, but not by the programmer.
In your mind... ;-)

Hmm, haven't you noticed that there doesn't seem to be any other regular
here that shares your view on this topic, and that you get involoved in
discussions of that kind regularly because each time several others think
that you're explaining incorrect things to novice programmers? Have you
ever considered that the misconception might be in your mind, not in the
minds of everyone else?
Maybe we should take that discussion to comp.std.c++ and hear what the
standards people have to say about it, to end this once and for all.
 
A

Alf P. Steinbach

* Rolf Magnus:
Alf said:
* Victor Bazarov:
[...]
Every tree of constructor calls start with a source code level
constructor call, it's just a very common misconception that they're
never "explicit", or not "calls" (that last misconception is mentioned
in the FAQ, by ref to the definition of default constructor). The
first misconception probably stems from a passage in the standard where
the word "explicit" is used for disambiguation in a particular context,
and the last misconception probably stems from the desire to teach
newbies not to think of a constructor as a an ordinary callable member
function, but doing that by inventing a simple and incorrect
explanation instead of how things actually work.

12.1/2 "Because the constructors do not have names, they are never found
during name lookup". Yes, you can _cause_ a constructor to be called.

That's correct.
Yes.
You can never _call_ one, though.

That's incorrect.

Let's see how a C++ programmer calls a function. I see two ways:

1. Write function's name, followed by '(', a parameter list and ')'.
2. Through a pointer, which is similar, except that instead of the
function's name, you use a pointer to it. However, to make a pointer point
to it, you need to have the function's name too.

To make the list of non-constructor calls complete you would have to
include destructor calls, conversion operator calls, other operator
calls, and allocation and deallocation function calls.

It's irrelevant how you call non-constructors.

[more irrelevant details + emotional appeal, snipped].

Yes, of course it can be called, but not by the programmer.

The passage I referred to refers to a source-code level call, and is
meaningless under any other interpretation: "can be called without an
argument" only makes sense at the source code level. If you think there
is any other reasonable interpretation do supply it. And this is a FAQ
because so many (e.g. you) persist in a silly misconception: it's always
a good idea to read, or at least skim, the FAQ before posting.
 
R

Rolf Magnus

Alf said:
To make the list of non-constructor calls complete you would have to
include destructor calls, conversion operator calls, other operator
calls, and allocation and deallocation function calls.

It's irrelevant how you call non-constructors.

[more irrelevant details + emotional appeal, snipped].

It's not emotional, it's logical. If everyone else has another opinion than
you, that doesn't automatically mean that everyone else is wrong. It could
also mean (and is more likely) that you are the one who is wrong. It seems
you're not even considering that.
The passage I referred to refers to a source-code level call, and is
meaningless under any other interpretation: "can be called without an
argument" only makes sense at the source code level. If you think there is
any other reasonable interpretation do supply it.

Well, the arguments you specify when you create an object are forwareded to
the constructor.
And this is a FAQ because so many (e.g. you) persist in a silly
misconception: it's always a good idea to read, or at least skim, the FAQ
before posting.

Could you tell me where that is? I looked in section 10 (Constructors) and
several other sections, but couldn't find it.
 
A

Alf P. Steinbach

* Rolf Magnus:
* Alf P. Steinbach:


Well, the arguments you specify when you create an object are forwareded to
the constructor.

A = "can be called without an argument" means, according to you,

B = "the arguments you specify when you create an object are
forwareded to the constructor"

Please explain how statement A really means B, and how statement B is a
good definition of a default constructor.

Then we can go back up the digression stack and you can then explain
what "called" means in statement A (the standard's wording).
 
R

Rolf Magnus

Alf said:
* Rolf Magnus:

A = "can be called without an argument" means, according to you,

B = "the arguments you specify when you create an object are
forwareded to the constructor"

No, and I didn't claim so.
Please explain how statement A really means B,

A doesn't mean B. B just explains how A can be true even though you can't
call a constructor yourself.
If you specify arguments on creation of an object, those argments are
forwarded to the constructor. If you don't, well - there isn't anything to
forward, so the default constructor gets called.
and how statement B is a good definition of a default constructor.

It isn't.
Then we can go back up the digression stack and you can then explain
what "called" means in statement A (the standard's wording).

If the program stops execution of whatever it was doing, jumps to the
beginning of the constructor's body, executes it, then jumps back and
continues what it was doing before, then I'd say that the constructor was
"called".
 
A

Alf P. Steinbach

* Rolf Magnus:
No, and I didn't claim so.


A doesn't mean B. B just explains how A can be true even though you can't
call a constructor yourself.
If you specify arguments on creation of an object, those argments are
forwarded to the constructor. If you don't, well - there isn't anything to
forward, so the default constructor gets called.


It isn't.


If the program stops execution of whatever it was doing, jumps to the
beginning of the constructor's body, executes it, then jumps back and
continues what it was doing before, then I'd say that the constructor was
"called".

Your interpretation of §12.1/5 is then that a default constructor is one
that isn't forwarded any arguments.

That is incorrect.

A default constructor is, quoting the standard, one that "can be called
without an argument"; it can have any number of defaulted formal
arguments (which are forwarded to it).
 
R

Rolf Magnus

Alf said:
Your interpretation of §12.1/5 is then that a default constructor is one
that isn't forwarded any arguments.

Not exactly. Because you didn't specify any arguments, there are none to
forward to the constructor. Therefore, only a constructor may be used that
can be called without an argument.
That is incorrect.

It's not exactly what I said.
A default constructor is, quoting the standard, one that "can be called
without an argument";

Note that I inserted exactly that into my explanation above.
 
K

Karl Heinz Buchegger

Rolf said:
Not exactly. Because you didn't specify any arguments, there are none to
forward to the constructor. Therefore, only a constructor may be used that
can be called without an argument.


It's not exactly what I said.


Note that I inserted exactly that into my explanation above.


Ralf, give up.
Many of the regulars had this discussion with Alf a number of times.
He doesn't understand or he will not understand, that this section of
the standard isn't phrased very well. From the rest of the standards
document however it is very clear what is ment, at least for most of
us.
 
A

Alf P. Steinbach

* Rolf Magnus:
Not exactly. Because you didn't specify any arguments, there are none to
forward to the constructor.

I note in passing that here you interpreted "called without an argument"
as "you didn't specify any arguments", i.e. "called" = source code call.

Which is correct, but the conclusion after the comma is incorrect.

A default constructor can have any number of defaulted formal arguments,
and those will be forwarded to it.

Therefore, only a constructor may be used that
can be called without an argument.

Do you mean

a) only a constructor that can be called at the source code level
without an argument (correct interpretation),
or

b) only a constructor that can be called at the generated call
level without an argument (if it has formal arguments then in any
reasonable C++ implementation it can't be called at that level
without arguments, or otherwise it means a default constructor is
defined in terms of a call to an unspecified and unmentioned thunk
mechanism that supplies the actual arguments and also can be used
for _any_ constructor, so that the definition is void)

?

It's not exactly what I said.

Glad it wasn't.

Note that I inserted exactly that into my explanation above.

Yes, the question here is whether you can provide a reasonable and clear
interpretation of what "called" means in that statement, one that is
compatible with your other (unsustainable) views; you haven't done so.
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
Ralf, give up.

Good advice.

Many of the regulars had this discussion with Alf a number of times.
He doesn't understand or he will not understand, that this section of
the standard isn't phrased very well. From the rest of the standards
document however it is very clear what is ment, at least for most of
us.

By "isn't phrased very well" do you mean the standard incorrectly uses
the word "call" about constructor calls?

LOL. :)
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Karl Heinz Buchegger:

Good advice.



By "isn't phrased very well" do you mean the standard incorrectly uses
the word "call" about constructor calls?

LOL. :)

Uhm, just out of curiosity, please explain what this note means (quoting
the Holy Standard): "explicit constructor calls do not yield lvalues"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top