https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77615
Bug ID: 77615
Summary: Template specialization is resolved differently with
and without optimizations turned on
Product: gcc
Version: 6.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jan.bujak at nokia dot com
Target Milestone: ---
Created attachment 39632
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39632&action=edit
Reproduction testcase
Please consider the following code:
// shared.h
#include <stdio.h>
template <typename T>
void hello() {
printf( "generic\n" );
}
// specialization.cc
#include "shared.h"
template <>
void hello< int >() {
printf( "specialized\n" );
}
// usage.cc
#include "shared.h"
int main() {
hello< int >();
return 0;
}
Now, if we compile it like this:
g++ -Wall -Wextra -O0 specialization.cc usage.cc
then the specialized version will be called; if we compile like this:
g++ -Wall -Wextra -O1 specialization.cc usage.cc
then the generic version will be called.
The order of compilation doesn't seem to matter. The behavior is the same for
any other optimization level. When compiled on clang in both cases the
specialized version is called.
Tested on the following GCC versions on Arch Linux:
g++ (GCC) 6.2.1 20160830
g++ (GCC) 6.1.1 20160501