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
          Reporter: jan.bujak at nokia dot com
  Target Milestone: ---

Created attachment 39632
Reproduction testcase

Please consider the following code:

// shared.h
#include <stdio.h>

template <typename T>
void hello() {
    printf( "generic\n" );

#include "shared.h"

template <>
void hello< int >() {
    printf( "specialized\n" );

#include "shared.h"

int main() {
    hello< int >();
    return 0;

Now, if we compile it like this:

g++ -Wall -Wextra -O0

then the specialized version will be called; if we compile like this:

g++ -Wall -Wextra -O1

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

Reply via email to