473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bug in precedence for built-in types?

I would expect the output to be:

5
post increment
assign
5

This is the case for Visual c++ 6 AND g++ 3.4.2. However, for visual c++
7.1, I get:

6
post increment
assign
5

Any ideas? Is this undefined behaviour? I think its pretty well defined. But
in either case, its inconsistent for v7.1 between built-ins and udt's!

#include <cassert>
#include <iostream>

struct test
{
int me;
test(int mee):me(mee){}
test operator++(int)
{
test a(*this);
++me;
std::cout << "post increment" << std::endl;
return a;
}

operator int(){return me;}

test & operator=(const test & rhs)
{
if(&rhs!=this) std::cout << "assign" << std::endl;
this->me = rhs.me;
return *this;
}
};

template<typename T>
void doit()
{
T aaa(T(5));
aaa = aaa++;
std::cout << int(aaa) << std::endl;
}

int main()
{
doit<char *>();
doit<test>();
}
Apr 5 '06 #1
4 1036
For the built-in type it isn't calling your post-increment function. With a
function call there would be some sequence points associated with the
function call, but without a function call those sequence points do not
exist. Without those sequence points, you got off lucky. This line of
code:
aaa = aaa++;
is allowed to format your hard drive.

"Sean Connery" <Se*********@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
I would expect the output to be:

5
post increment
assign
5

This is the case for Visual c++ 6 AND g++ 3.4.2. However, for visual c++
7.1, I get:

6
post increment
assign
5

Any ideas? Is this undefined behaviour? I think its pretty well defined.
But
in either case, its inconsistent for v7.1 between built-ins and udt's!

#include <cassert>
#include <iostream>

struct test
{
int me;
test(int mee):me(mee){}
test operator++(int)
{
test a(*this);
++me;
std::cout << "post increment" << std::endl;
return a;
}

operator int(){return me;}

test & operator=(const test & rhs)
{
if(&rhs!=this) std::cout << "assign" << std::endl;
this->me = rhs.me;
return *this;
}
};

template<typename T>
void doit()
{
T aaa(T(5));
aaa = aaa++;
std::cout << int(aaa) << std::endl;
}

int main()
{
doit<char *>();
doit<test>();
}


Apr 6 '06 #2
Norman Diamond wrote:
For the built-in type it isn't calling your post-increment function. With
a function call there would be some sequence points associated
with the function call, but without a function call those sequence
points do not exist. Without those sequence points, you got off
lucky. This line of code:
aaa = aaa++;
is allowed to format your hard drive.


To expand on that a little bit -

aaa = aaa++;

attempts to modify a variable twice between sequences points if the typeof
aaa is a built-in type but not if it's a class type.

See this recent thread

http://groups.google.com/group/micro...734ae4cab7c23b

for a lot of discussion as to why this is undefined behavior and what all
that means.

-cd
Apr 6 '06 #3


"Norman Diamond" wrote:
This line of
code:
aaa = aaa++;
is allowed to format your hard drive.


Hold on, so you're telling me it *wasn't* a virus?!
Apr 12 '06 #4
"Sean Connery" <Se*********@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
"Norman Diamond" wrote:
This line of
code:
aaa = aaa++;
is allowed to format your hard drive.


Hold on, so you're telling me it *wasn't* a virus?!


It's allowed to be a virus. It has a licence to kill ^u^

Apr 12 '06 #5

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

Similar topics

2
by: Robert | last post by:
Hi, According to the operator precedence table, the dot operator has a higher precedence than the 'new' operator: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html ...
8
by: gouqizi.lvcha | last post by:
I have a statement as follows, a = b++; why b=b+1 after a=b. I check the C language precedence (K&R Page 52) , ++ should has higher precedence than = . Rick
5
by: Angel Tsankov | last post by:
Can someone give a reason why bitwise operators (&,^,|) have lower precedence than equality/inequality test oeprators (==, !=)?
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
3
by: maadhuu | last post by:
hi, i am a bit confused as to how *i++ (i is a pointer) works.....the postfix + has a higher precedence than prefix ++ and i think precedence is from left to right whereas ,prefix ++ and * have...
11
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
5
by: mrpermuted | last post by:
Hello, Could someone explain to me why the postfix ++ operator is listed near the top of the operator precedence (above even prefix ++ on my book's chart) but is not evaluated before operators...
4
by: Toni | last post by:
Hi, This has probably debated here before but I could not find it in Google. Though I don't have the standard at hand I've seen several references point that operator -> has higher precedence...
36
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.