473,804 Members | 3,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lvalue difference between static and non static member functions

I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}

Feb 5 '06 #1
9 3780
tk****@gmail.co m wrote:
I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}

You are passing a non-const reference to a temporary object (which
doesn't make sense - what happens if you change it?), you should either
declare g taking a const A&, or pass a real A.

--
Ian Collins.
Feb 5 '06 #2


tk****@gmail.co m wrote:
I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}


The error isn't related to the functions... what it is complaining about
is the fact that A() is not an lvalue, but you are using it to initialize
a non-const reference variable (in this case a parameter). Why it would
do that I don't know... the compiler I use allows such things by liberally
creating temporary variables on an as-needed basis, then using the lvalue
of the temp.

I would like to hear if the comeau behavior is standard behavior, but
meanwhile you can fix it by changing the declaration of g as follows:

static void g(const A& a);

David

Feb 5 '06 #3
Ian Collins wrote:
tk****@gmail.co m wrote:
I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}
You are passing a non-const reference to a temporary object (which
doesn't make sense - what happens if you change it?),


Well, it makes sense. With all objects, changes occur only during the
lifetime of the object. That this lifetime is short (in the case of a
temporary), does not change anything. In fact, think of a file stream.
Clearly you could write stuff to the file while it exists. Once the
temporary file stream dies, the file closes and the changes are now on your
hard disk:

#include <iomanip>
#include <fstream>

int main ( void ) {

std::ofstream( "/dev/stdout" ) << "hello world!\n";

}

Note that this works, since you can call non-const member functions of
temporary objects.

However, although it makes sense to modify a temporary, the standard simply
forbids passing a non-const reference to a temporary. I never found a
convincing rational.

you should either declare g taking a const A&, or pass a real A.


Correct.
Best

Kai-Uwe Bux
Feb 5 '06 #4
David Lindauer wrote:
The error isn't related to the functions... what it is complaining about
is the fact that A() is not an lvalue, but you are using it to initialize
a non-const reference variable (in this case a parameter). Why it would
do that I don't know... the compiler I use allows such things by liberally
creating temporary variables on an as-needed basis, then using the lvalue
of the temp.

I would like to hear if the comeau behavior is standard behavior, but
meanwhile you can fix it by changing the declaration of g as follows:

It is. Several compilers (mine included) do as yours does, but they
shouldn't.

--
Ian Collins.
Feb 5 '06 #5
Kai-Uwe Bux wrote:
Ian Collins wrote:
tk****@gmail.co m wrote:
I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}
You are passing a non-const reference to a temporary object (which
doesn't make sense - what happens if you change it?),


Well, it makes sense. With all objects, changes occur only during the
lifetime of the object. That this lifetime is short (in the case of a
temporary), does not change anything. In fact, think of a file stream.
Clearly you could write stuff to the file while it exists. Once the
temporary file stream dies, the file closes and the changes are now on
your hard disk:

#include <iomanip>
#include <fstream>

int main ( void ) {

std::ofstream( "/dev/stdout" ) << "hello world!\n";


Hah! Tricked myself again. Should be:

std::ofstream( "/dev/stdout" ) << std::dec << "hello world!\n";
}

Note that this works, since you can call non-const member functions of
temporary objects.

However, although it makes sense to modify a temporary, the standard
simply forbids passing a non-const reference to a temporary. I never found
a convincing rational.

you should either declare g taking a const A&, or pass a real A.


Correct.
Best

Kai-Uwe Bux


Feb 6 '06 #6
Kai-Uwe Bux wrote:
Ian Collins wrote:

tk****@gmail. com wrote:
I compiled the source below using the comeau compiler
http://www.comeaucomputing.com/tryitout/
Why does the f funtion compile while the g function produces an lvalue
error ?
(I thought the two functions were identical except for a harmless
syntax difference)

struct A
{
void f(){}
static void g(A& a){}
};
int main()
{
A().f();
A::g(A()); // must be an lvalue
}


You are passing a non-const reference to a temporary object (which
doesn't make sense - what happens if you change it?),

Well, it makes sense. With all objects, changes occur only during the
lifetime of the object. That this lifetime is short (in the case of a
temporary), does not change anything. In fact, think of a file stream.
Clearly you could write stuff to the file while it exists. Once the
temporary file stream dies, the file closes and the changes are now on your
hard disk:

Yes, I know it was a lame explanation, but it was the best I could come
up with. I've never fully understood this. Caused me all sorts of
grief porting code from a compiler that ignores the rule to one that
enforces it....

--
Ian Collins.
Feb 6 '06 #7
Ian Collins wrote:
[...] I've never fully understood this. Caused me all sorts
of grief porting code from a compiler that ignores the rule to one
that enforces it....


D&E, page 82, IIRC. Or 86. I guess I don't RC...

V
--
Please remove capital As from my address when replying by mail
Feb 6 '06 #8
Sorry I do not feel my question has been completely answered. I am not
looking for a work-around to the compiler error. I am asking why they
chose to design the language such that the f function call does compile
while the g function call does not.

(if the answer is in D&E I will be happy to hear about it)

Feb 6 '06 #9
On Mon, 06 Feb 2006 01:54:41 -0800, Thomas Krog wrote:
Sorry I do not feel my question has been completely answered. I am not
looking for a work-around to the compiler error. I am asking why they
chose to design the language such that the f function call does compile
while the g function call does not.

(if the answer is in D&E I will be happy to hear about it)


I can give you half and answer... My 1990 (has it really been that long?)
copy of the Annotated C++ Reference Manual (sec 8.4.3 p 155) says:

The distinction was made to eliminate a major source of errors and
surprises. Earlier, all references could be initialized to refer to
temporary objects...

The other half, examples of the major errors and surprises, escapes me.
Anyone?

--
Ben.

Feb 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3501
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
4624
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
9
3559
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form (lex.literal). A string literal is an *lvalue*; all other literals are *rvalues*. -4- The operator :: followed by an identifier, a qualified-id, or an operator-function-id is a primary-expression. Its type is specified by the
14
3004
by: Michael Ovetsky | last post by:
Consider: int g(){int b=1; return b;} int main() { int d=2; g()=d; }
15
6024
by: Youssef Mesri | last post by:
What is the difference between these two situations: 1- hold a namespace which contains justs some functions: namespace MyNamespace { void foo1(); void foo2(); void foo3(); void foo4();
3
5771
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that anything is lvalue and if cannot assign to anything then that anything is not lvalue.
2
1774
by: subramanian100in | last post by:
In C++ Primer(4th Edition) by Stanley Lippman, in page 162, the following is mentioned: The general syntactic form of a compound assignment operator is a op= b where op= may be one of the following ten operators: += -= *= /= %= // arithmetic operators <<= >>= &= ^= != // bitwise operators
1
1284
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <cstdlib> using namespace std; class Test { public:
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.