temporory objects are constant ?

M

MagnumOpus

Hello all,

I have a question on temporary objects.
Are the temporary objects always constant ?

class X {
int i;
public:
X (int ii = 0);
void modify();
};

X::X(int ii) { i = ii; }

void X::modify() { i = i++; }

X f5() { return X(); }

void f7(X& x) { //return by const reference
x.modify();
}

void main()
{
f7(f5());
}

As per Bruce Eckel, temporary objects are always constant.
But when i tried to compile this prograrm in VS, it doesn't complain
me at all.

Can anybody explain about this ?

Thank you
Sunil
 
I

Ian Collins

Hello all,

I have a question on temporary objects.
Are the temporary objects always constant ?

class X {
int i;
public:
X (int ii = 0);
void modify();
};

X::X(int ii) { i = ii; }

void X::modify() { i = i++; }

X f5() { return X(); }

void f7(X& x) { //return by const reference

This comment doesn't make sense; the function does not return anything!
x.modify();
}

void main()
{
f7(f5());
}

As per Bruce Eckel, temporary objects are always constant.
But when i tried to compile this prograrm in VS, it doesn't complain
me at all.

The compiler isn't enforcing the rule (is it a recent version?). It is
invalid to initialise a non-const reference with a temporary.

The compiler should be complaining about the incorrect definition of main.
 
A

Alf P. Steinbach /Usenet

* MagnumOpus, on 06.07.2010 04:08:
I have a question on temporary objects.
Are the temporary objects always constant ?
No.


class X {
int i;
public:
X (int ii = 0);
void modify();
};

X::X(int ii) { i = ii; }

void X::modify() { i = i++; }

X f5() { return X(); }

void f7(X& x) { //return by const reference
x.modify();
}

void main()

This should not compile, since 'main' must have 'int' result type.

At a guess you're using MSVC.

{
f7(f5());

This should not compile, since a temporary can't implicitly convert to reference
to non-const.

At a guess you're using MSVC.

}

As per Bruce Eckel, temporary objects are always constant.

Probably that is is not what he's written.

But when i tried to compile this prograrm in VS, it doesn't complain
me at all.

Can anybody explain about this ?

First, temporary objects are not const, except those you explicitly create as
const. Your code above should still not compile. That it does compile probably
means that you're using MSVC.

You should consider upping the MSVC warning level to at least /W3.


Cheers & hth.,

- ALf
 
Ö

Öö Tiib

Hello all,

I have a question on temporary objects.
Are the temporary objects always constant ?

class X {
        int i;
public:
        X (int ii = 0);
        void modify();

};

X::X(int ii) { i = ii; }

void X::modify() { i = i++; }

X f5() { return X(); }

void f7(X& x) { //return by const reference
        x.modify();

}

void main()
{
    f7(f5());

}

As per Bruce Eckel, temporary objects are always constant.
But when i tried to compile this prograrm in VS, it doesn't complain
me at all.

Can anybody explain about this ?

VS gives a warning (at warning level 4):

warning C4239: nonstandard extension used : 'argument' : conversion
from 'X' to 'X &'

Typical VS. Important warnings need level 4; worthless sillywarnings
are given at level 3. Use always /W4.
 
Ö

Öö Tiib

VS gives a warning (at warning level 4):

 warning C4239: nonstandard extension used : 'argument' : conversion
from 'X' to 'X &'

Did not somehow paste important part of warning text:

A non-const reference may only be bound to an lvalue
 
R

Rolf Magnus

MagnumOpus said:
Hello all,

I have a question on temporary objects.
Are the temporary objects always constant ?

No.

struct X
{
int i;
};

int main()
{
X().i = 5;
}

As per Bruce Eckel, temporary objects are always constant.

I heavily doubt that.
But when i tried to compile this prograrm in VS, it doesn't complain
me at all.

Your compiler is probably not configured correctly. You might need to set it
to "standard compliant mode" or something like that.
 
M

MagnumOpus

* MagnumOpus, on 06.07.2010 04:08:









This should not compile, since 'main' must have 'int' result type.

At a guess you're using MSVC.


This should not compile, since a temporary can't implicitly convert to reference
to non-const.

At a guess you're using MSVC.



Probably that is is not what he's written.



First, temporaryobjectsare not const, except those you explicitly create as
const. Your code above should still not compile. That it does compile probably
means that you're using MSVC.

You should consider upping the MSVC warning level to at least /W3.

Cheers & hth.,

- ALf

Hi

The below is the program that i got it from Bruce Eckel and i comiled
it in MSVC.

/////////////////////////////////////////////////////////////////////

class X {
int i;
public:
X(int ii = 0);
void modify();
};
X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() {
return X();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
} ///:~

///////////////////////////////////////////////////////

But when I uncommented f7(f5()), it doesn't complain me any errors.
However, when i change the warning level to /W4, it says
warning C4239: nonstandard extension used : 'argument' : conversion
from 'X' to 'X &'

I was expecting a error though.
 
M

MagnumOpus

* MagnumOpus, on 06.07.2010 04:08:









This should not compile, since 'main' must have 'int' result type.

At a guess you're using MSVC.


This should not compile, since a temporary can't implicitly convert to reference
to non-const.

At a guess you're using MSVC.



Probably that is is not what he's written.



First, temporary objects are not const, except those you explicitly create as
const. Your code above should still not compile. That it does compile probably
means that you're using MSVC.

You should consider upping the MSVC warning level to at least /W3.

Cheers & hth.,

- ALf

Please refer to Bruce Eckel's Thinking in c++, Constants chapter which
clearly says

"But there is one thing about temporaries: they’re
automatically const."

Let me know if my understanding is wrong.
 
B

Bart van Ingen Schenau

Hi

The below is the program that i got it from Bruce Eckel and i comiled
it in MSVC.

/////////////////////////////////////////////////////////////////////

class X {
int i;
public:
X(int ii = 0);
void modify();};

X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() {
return X();}

const X f6() {
return X();}

void f7(X& x) { // Pass by non-const reference
x.modify();}

int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());

} ///:~

///////////////////////////////////////////////////////

But when I uncommented f7(f5()), it doesn't complain me any errors.
However, when i change the warning level to /W4, it says
warning C4239: nonstandard extension used : 'argument' : conversion
from 'X' to 'X &'

I was expecting a error though.

Most compilers will give an error for that code, but Microsoft has
decided, in all its wisdom, that they want to (continue to) support
this kind of code.
This decision directly implies that the strongest type of complaint
they will give will be a warning message, and not an error.
The reason is that, by convention, if the compiler gives out one or
more error messages for a file, then the compiler will not produce an
object file or any other form of executable code.

For standard compliance, it is sufficient that a compiler produces
some kind of diagnostic message, but the standard does not
differentiate between errors, warnings and other informational
messages.

Bart v Ingen Schenau
 
M

MagnumOpus

Most compilers will give an error for that code, but Microsoft has
decided, in all its wisdom, that they want to (continue to) support
this kind of code.
This decision directly implies that the strongest type of complaint
they will give will be a warning message, and not an error.
The reason is that, by convention, if the compiler gives out one or
more error messages for a file, then the compiler will not produce an
object file or any other form of executable code.

For standard compliance, it is sufficient that a compiler produces
some kind of diagnostic message, but the standard does not
differentiate between errors, warnings and other informational
messages.

Bart v Ingen Schenau- Hide quoted text -

- Show quoted text -

Thank you for the information.
I will try with other compilers also.
 
A

Alf P. Steinbach /Usenet

* MagnumOpus, on 06.07.2010 11:11:
Please refer to Bruce Eckel's Thinking in c++, Constants chapter which
clearly says

"But there is one thing about temporaries: they’re
automatically const."

Let me know if my understanding is wrong.

Yes, he is. :)

I'm attempting to CC this to Bruce Eckel so that he can put it in his errata
list, assuming there is one. Unfortunately I don't have his private address in
Thunderbird (I probably had it once) and his websites seem to be down. I found a
generic "business" address by looking at his pages via the Wayback Engine's
archive, and he goes through some hoops to discourage readers from sending him
mail, apparently as a popular author he gets tons of mails; if you need it it's
MindViewInc as username for gmail address, and as it turned out /that/ address
was in my Thunderbird address list, no mention of Bruce though :)

"Thinking in C++" is great for getting up to speed, but like all books it has erors.

Example:


<code>
struct S { int x; };

S foo() { S o = {1234}; return o; }

int main()
{
foo() = S();
}
</code>


This code should compile because even though foo returns a temporary it's a
temporary of class type, and you can call methods, in particular the
automatically generated assignment operator, even on temporary class type
objects. If the object was const, though, this should not compile. One cannot
assign to a const object unless it has a user-defined assignment operator that
is const (this is seldom done, but sometimes for proxy objects).


Cheers & hth.,

- Alf
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top