J
jason.cipriani
First I have a question about the "official" definition of a
"temporary object".
So in this code:
void function1 (SomeThing a) { }
void function2 (void) {
SomeThing b;
function1(b);
}
A copy of b is made to pass to function1(). That's a temporary object,
of course. I was always under the impression that a "temporary object"
was an object created by the compiler implicitly, and it's entirely
"behind the scenes" (as in the above example). But, is the object
created in the following code also called a "temporary object":
void function1 (SomeThing &a) { }
void function2 (void) {
function1(SomeThing());
}
I mean, you explicitly construct it. It's not secretly created by the
compiler -- but it is anonymous (outside of function1), and it does
get destroyed after the statement is evaluated. It's just not
completely "behind the scenes". Would that also be called, officially,
a "temporary object"?
My second question is about l-values. In the following code:
struct A {
A (void) { }
};
void function (void) {
A a;
A() = a;
}
That compiles on all the compilers I've tested it on (Borland's BCC32,
GCC, and Comeau); and I expected it to. That would mean that "A()" is
not a temporary, by definition, since it is also an l-value. It seems
weird that I can say "A() = something;". I mean I know exactly what it
is doing, but it doesn't make sense to do it (just like trying to set
"2 = 3" doesn't really make much sense) (unless the = operator has
some other side effects that you want). Are the compilers I've tried
it on broken? Or is A() really an l-value there? Or is it compiler-
specific whether or not that is accepted or not? The reason I am
confused is because of code like this (untested, I'm just typing it in
this email hopefully I didn't screw up something important):
struct X {
};
struct Y {
Y (const X &) { }
};
void function (Y &) { }
void function2 (void) {
X x;
function(Y(x));
}
That code compiles with recent versions of GCC, Borland's compiler,
and the MS compiler. However, Comeau does not compile it, complaining
that non-const reference parameters to functions must be l-values. Yet
Comeau does compile the following (add a default constructor to Y in
the above example for this):
void function2 (void) {
Y y;
(Y(x)) = y;
}
Comeau's inconsistency about what it thinks an l-value is bugs me (I
asked them about this one and am waiting for a response).
So I'm a little confused. Mostly I'm asking to clear up some points in
a discussion I'm having with somebody on another newsgroup.
Thanks,
Jason
"temporary object".
So in this code:
void function1 (SomeThing a) { }
void function2 (void) {
SomeThing b;
function1(b);
}
A copy of b is made to pass to function1(). That's a temporary object,
of course. I was always under the impression that a "temporary object"
was an object created by the compiler implicitly, and it's entirely
"behind the scenes" (as in the above example). But, is the object
created in the following code also called a "temporary object":
void function1 (SomeThing &a) { }
void function2 (void) {
function1(SomeThing());
}
I mean, you explicitly construct it. It's not secretly created by the
compiler -- but it is anonymous (outside of function1), and it does
get destroyed after the statement is evaluated. It's just not
completely "behind the scenes". Would that also be called, officially,
a "temporary object"?
My second question is about l-values. In the following code:
struct A {
A (void) { }
};
void function (void) {
A a;
A() = a;
}
That compiles on all the compilers I've tested it on (Borland's BCC32,
GCC, and Comeau); and I expected it to. That would mean that "A()" is
not a temporary, by definition, since it is also an l-value. It seems
weird that I can say "A() = something;". I mean I know exactly what it
is doing, but it doesn't make sense to do it (just like trying to set
"2 = 3" doesn't really make much sense) (unless the = operator has
some other side effects that you want). Are the compilers I've tried
it on broken? Or is A() really an l-value there? Or is it compiler-
specific whether or not that is accepted or not? The reason I am
confused is because of code like this (untested, I'm just typing it in
this email hopefully I didn't screw up something important):
struct X {
};
struct Y {
Y (const X &) { }
};
void function (Y &) { }
void function2 (void) {
X x;
function(Y(x));
}
That code compiles with recent versions of GCC, Borland's compiler,
and the MS compiler. However, Comeau does not compile it, complaining
that non-const reference parameters to functions must be l-values. Yet
Comeau does compile the following (add a default constructor to Y in
the above example for this):
void function2 (void) {
Y y;
(Y(x)) = y;
}
Comeau's inconsistency about what it thinks an l-value is bugs me (I
asked them about this one and am waiting for a response).
So I'm a little confused. Mostly I'm asking to clear up some points in
a discussion I'm having with somebody on another newsgroup.
Thanks,
Jason