Bug 45349 - template function specializations are not recognized when the template is implemented in a header, and optimization is on.
Summary: template function specializations are not recognized when the template is imp...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-19 21:34 UTC by Tyler
Modified: 2010-08-19 22:13 UTC (History)
1 user (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
tar.gz of source and temporaries (9.87 KB, application/octet-stream)
2010-08-19 21:36 UTC, Tyler
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tyler 2010-08-19 21:34:18 UTC
Define a prototype for a template function in a class definition and include the default implementation in the header.
In a separate cpp file, create a specialization of the template function.

When the function is called that matches the specialization, normally the specialized function is called, unless the g++ optimizations (-O<n> ) are turned on.  Then the specialization is out of scope, and the default template implementation is called:

compile the attached code as follows:

g++ special.cpp smain.cpp

then run the result:

./a.out 

# Specialized! 
# Template function!

Now do the same with optimization:

g++ -O3 special.cpp smain.cpp

and run again:

./a.out 

# Specialized! 
# Template function!

With optimization on, the specialization is not called.
NOTE: This does NOT occur if the class declaration is in the source modue (.cpp), only when it's included from an external header (.h).
Comment 1 Tyler 2010-08-19 21:36:15 UTC
Created attachment 21524 [details]
tar.gz of source and temporaries

source tarball
Comment 2 Andrew Pinski 2010-08-19 21:38:09 UTC
This is invalid code IIRC and does not requires a diagnostic.
Comment 3 Tyler 2010-08-19 21:40:34 UTC
Also included in the attachment is s2.cpp wich is an duplicate example of the specialization in a single module.  This sample when compiled does NOT exhibit the same problem as when the class declaration (and template function) occurs in a header.

Comment 4 Andrew Pinski 2010-08-19 21:41:54 UTC
specialization is required to be done visible in the translation unit when the use is used IIRC.
Comment 5 Tyler 2010-08-19 21:46:38 UTC
(In reply to comment #4)
> specialization is required to be done visible in the translation unit when the
> use is used IIRC.
> 

Makes sense, however I would assume it would work the same way when optimized (-O3).  Is that not correct?
Comment 6 Richard Biener 2010-08-19 21:53:25 UTC
No, it's not.  Your testcase violates the ODR and inlining makes that visible.
Comment 7 Richard Biener 2010-08-19 21:53:41 UTC
Thus, invalid.
Comment 8 Tyler 2010-08-19 22:13:58 UTC
(In reply to comment #7)
> Thus, invalid.
> 

understood.  Thanks.