User account creation filtered due to spam.

Bug 18468 - bad code in template function called from template class method
Summary: bad code in template function called from template class method
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-13 20:30 UTC by Jim Avera
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i586-suse-linux-gnu
Target: i586-suse-linux-gnu
Build: i586-suse-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test case (1.51 KB, text/x-c++)
2004-11-13 20:32 UTC, Jim Avera
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jim Avera 2004-11-13 20:30:55 UTC
A template function (which swaps two values)
is called in a loop in an inlined method of a template class.  The number
of loop iterations is a constant, so the compiler might be trying to unroll
the loop.

The final assignment of the swap function is not executed after the
first iteration, causing incorrect results.

The problem occurs with -O2 or -O3, but not with -O.

I will attach a self-contained demo program.
Comment 1 Jim Avera 2004-11-13 20:32:09 UTC
Created attachment 7542 [details]
test case

Please compile with -O2
Comment 2 Andrew Pinski 2004-11-13 20:37:05 UTC
You are violating the C++ aliasing rules:

class GetPtrtokeyFunctor {
    public: 
    const PtrInt * operator()(const Objptr & objptr)
            { return reinterpret_cast<const PtrInt *>(&objptr); }
};

Comment 3 Jim Avera 2004-11-16 02:47:34 UTC
Subject: Re:  bad code in template function called from template class method

Hi,
Can you summarize what aliasing rule is violated?

Note that the argument is a ref, so the code is
not taking the adderess of a temporary.

Thanks,
-Jim

--- pinskia at gcc dot gnu dot org <gcc-bugzilla@gcc.gnu.org>
wrote:

> 
> ------- Additional Comments From pinskia at gcc dot gnu dot org 
> 2004-11-13 20:37 -------
> You are violating the C++ aliasing rules:
> 
> class GetPtrtokeyFunctor {
>     public: 
>     const PtrInt * operator()(const Objptr & objptr)
>             { return reinterpret_cast<const PtrInt *>(&objptr); }
> };
> 
> 
> 
> -- 
>            What    |Removed                     |Added
>
----------------------------------------------------------------------------
>              Status|UNCONFIRMED                 |RESOLVED
>          Resolution|                            |INVALID
> 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18468
> 
> ------- You are receiving this mail because: -------
> You reported the bug, or are watching the reporter.
> 


=====
__________________________________________________
Please do not put me on a mailing list or give my 
address(es) to anyone else.


		
__________________________________ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 

Comment 4 Andrew Pinski 2004-11-16 02:52:11 UTC
Read http://gcc.gnu.org/bugs.html#nonbugs_general

Casting does not work as expected when optimization is turned on.
Comment 5 Andrew Pinski 2005-06-05 09:04:34 UTC
Reopening to ...
Comment 6 Andrew Pinski 2005-06-05 09:04:52 UTC
Mark as a dup of bug 21920

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