User account creation filtered due to spam.

Bug 32067 - template<class T> XOR-Swap(T &a, T &b); error with -O0 option
Summary: template<class T> XOR-Swap(T &a, T &b); error with -O0 option
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.2
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-24 12:16 UTC by Nan Wang
Modified: 2007-05-29 10:11 UTC (History)
52 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nan Wang 2007-05-24 12:16:01 UTC
It gives an unexpected result while doing xor operation on a templated class.

For sample, here is my simple code:
------------------------------------------------------
#include <iostream>

using namespace std;

template
<class T>
inline void XOR-Swap(T &a, T &b)
{
        if (a != b)
        a ^= b ^= a ^= b;
};

int main()
{
        int a = 10;
        int b = -3;
        XOR-Swap(a, b);

        cout << a << endl;
        cout << b << endl;

        return 0;
}
-----------------------------------------------------
Results:

1. with "-O0": Incorrect
-------------------
0
10
-------------------


2. with "-O1": Correct
-------------------
-3
10
-------------------


Any comment?
Comment 1 Pawel Sikora 2007-05-24 12:31:39 UTC
(In reply to comment #0)

> Any comment?

this is an example of undefined behaviour.
http://blogs.msdn.com/rick_schaut/archive/2004/03/06/85357.aspx
Comment 2 Andrew Pinski 2007-05-24 16:21:25 UTC
http://gcc.gnu.org/bugs.html#nonbugs_c

*** This bug has been marked as a duplicate of 11751 ***