473,788 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify variable twice between two sequence points

I'm looking at an assignment where the students are expected to write a
class with a given purpose. According to the assignment, you should be
able to do this with instances of the class:
++instance_of_c lass;
instance_of_cla ss++;
(++instance_of_ class)++; // Isn't this invalid C++?

but not:
++(instance_of_ class++);

But doesn't the third thing the class should support,
(++instance_of_ class)++;, violate the rule that says that a variable
may not be modified more than once between two sequence points or else
undefined behaviour is the result? I'm not looking to start a heated
discussion here, just want confirmation or denial from the gurus here
before I let the person behind the assignment know about his mistake
(if, in fact, he made one).

/ Eric

Sep 24 '06 #1
2 2127
Eric Lilja posted:
I'm looking at an assignment where the students are expected to write a
class with a given purpose. According to the assignment, you should be
able to do this with instances of the class:
++instance_of_c lass;

The built-in preincrement operator yields an L-value.

It would be wise if the user-defined operator++(void ) were also implemented
to yield an L-value, although the Standard does not require this.

instance_of_cla ss++;

The built-in postincrement operator yields an R-value.

The user-defined operator++(int) can be implemented however you please, to
yield either an R-value or an L-value.

Personally, I never implement operator(int), I think it's too messy.

(++instance_of_ class)++; // Isn't this invalid C++?

This is valid code for a built-in type, and it would make sense for it to
be valid code for the user-defined type also, although this is not required
by the Standard.

By "valid", I mean that it must compile.

but not:
++(instance_of_ class++);

This will NOT compile for a built-in type, because it tries to increment an
R-value. Whether it works with an object of user-defined type depends upon
the way operator++(int) is written.

But doesn't the third thing the class should support,
(++instance_of_ class)++;, violate the rule that says that a variable
may not be modified more than once between two sequence points or else
undefined behaviour is the result? I'm not looking to start a heated
discussion here, just want confirmation or denial from the gurus here
before I let the person behind the assignment know about his mistake
(if, in fact, he made one).

Nice catch!

The answer to the question is: Syntactic Sugar.

The expression actually becomes:

obj.operator++( ).operator++(5) ;

There is a sequence point between the function calls.

It could be argued, however, that:

(++obj)++

is bogus because:

(1) It's nonsensical.
(2) It goes against the way built-in types can be used (assuming that a
sequence point violation actually occurs -- although I'm not 100% sure if
one does).

--

Frederick Gotham
Sep 24 '06 #2
On 24 Sep 2006 15:37:41 -0700 in comp.lang.c++, "Eric Lilja"
<mi********@gma il.comwrote,
>(++instance_of _class)++; // Isn't this invalid C++?
Your concern about sequence points mostly doesn't apply because a
function call is a sequence point. The built-in operators are not.

Sep 25 '06 #3

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

Similar topics

3
2389
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
4
1670
by: Timothy Madden | last post by:
Hello I've read a long time ago in the MSDN that C++ language defines no sequence points Now I read in the 1998 ISO standard a small list of sequence points in C++ Does C++ defines sequence points ? It really should if anyone asks me ... Thank you Timothy Madden
4
1756
by: Markus Dehmann | last post by:
I guess this is a kind of newbie question (since most pointer questions are newbie questions). In the program below, modify(string* s) is supposed to change the content that s points to. But the main() function shows that nothing is changed at all! What am I doing wrong?
28
12611
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one level3 argument for each level2 argument, etc. Suppose I code it similar to this fragment: int main( int argc, char *argv ) {
53
4098
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
41
10678
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
8
2765
by: TS | last post by:
Hi, i have inherited a page from another user. The page is the target frame in a frameset. for some reason the whole page runs twice (page_load, init, etc. all run twice) I can't figure out how this is happening. there are no transfers or redirects that are occurring in the pages. the page runs thru all of its events then starts all over again. Trying to inspect the stack track doesn't help because when i get to the init the 2nd time,...
7
1538
by: Eric Lilja | last post by:
...or does it violate the rule that a variable may not be modified more than once between two sequence points? int n = 0; std::cout << ++n << ' ' << n << ' ' << ++n << std::endl; - Eric
14
3460
by: subramanian100in | last post by:
In the following link, http://www.c-faq.com/malloc/retaggr.html The following ANSWER is given to a question(comp.lang.c FAQ list · Question 7.5a) : Whenever a function returns a pointer, make sure that the pointed-to memory is properly allocated. For example, make sure you have not done something like
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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
10173
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
9967
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
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
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
5399
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...
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.