This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc 4.1.1, template specialisation, -O3

On 6/13/06, Gavin Band <> wrote:
I hope this is the right place for this sort of question, which concerns
the behaviour of gcc (versions 3.4.4 and 4.1.1) and template

I have some code split into three files header.hpp, specialisation.cpp,
and main.cpp, given below.  A class having a template member function
print() is defined in "header.hpp", and a specialisation of print() for
std::strings is defined in "specialisation.cpp".  In "main.cpp" print()
is used to print an integer and then a string.

Without any optimisation turned on (i.e. compiling with the command line

"g++ -o test test.cpp specialisation.cpp")

the program output is

$ ./main
The object of type T was: 256.
The string was: hello.

If I compile with -O3, however, I get

$ ./main
The object of type T was: 256.
The object of type T was: hello.

Let me call the former behaviour "good" and the latter "bad" - here are
the optimisation flags which cause "good" and "bad" behaviour on my

gcc 3.4.4:  good: -O0, -O1 bad: -O2 or greater.
gcc 4.1.1:  good: -O0 bad: -O1 or greater

But perhaps it is not gcc but rather my code that is at fault?  This is
on a Linux box.  The code is below.

#include <iostream>
#include <string>
#include "header.hpp"

int main(int argc, char** argv)
  std::string s("hello");
  int i(256);

  A a;
  a.print(std::cerr, i);
  a.print(std::cerr, s);

  return 0;

You need to make a specialization declaration visible to main(), otherwise gcc will inline the not specialized version at -O and above.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]