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).


-- 
           Summary: template function specializations are not recognized
                    when the template is implemented in a header, and
                    optimization is on.
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tyler dot packer at landesk dot com
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45349

Reply via email to