Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > temporory objects are constant ?

Reply
Thread Tools

temporory objects are constant ?

 
 
MagnumOpus
Guest
Posts: n/a
 
      07-06-2010
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-06-2010
On 07/ 6/10 02:08 PM, MagnumOpus wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      07-06-2010
* 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


--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-06-2010
On 6 juuli, 05:08, MagnumOpus <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-06-2010
On 6 juuli, 05:42, Öö Tiib <(E-Mail Removed)> wrote:
> On 6 juuli, 05:08, MagnumOpus <(E-Mail Removed)> wrote:
>
>
>
> > 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 &'


Did not somehow paste important part of warning text:

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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-06-2010
MagnumOpus wrote:

> 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.

 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      07-06-2010
On Jul 6, 3:08*am, MagnumOpus <(E-Mail Removed)> wrote:

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


Don't do that... use one of { i = i+1; } or { ++i; } or { i++; }

 
Reply With Quote
 
MagnumOpus
Guest
Posts: n/a
 
      07-06-2010
On Jul 6, 7:25*am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> wrote:
> * MagnumOpus, on 06.07.2010 04:08:
>
>
>
> > I have a question on temporaryobjects.
> > Are the temporaryobjectsalwaysconstant?

>
> 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, temporaryobjectsare alwaysconstant.

>
> 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, 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
>
> --
> blog at <url:http://alfps.wordpress.com>- Hide quoted text -
>
> - Show quoted text -


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.
 
Reply With Quote
 
MagnumOpus
Guest
Posts: n/a
 
      07-06-2010
On Jul 6, 7:25*am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> wrote:
> * 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
>
> --
> blog at <url:http://alfps.wordpress.com>- Hide quoted text -
>
> - Show quoted text -


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.
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      07-06-2010
On Jul 6, 11:06*am, MagnumOpus <(E-Mail Removed)> wrote:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments