Bug 28271 - Specialization of a function is not called if compile with -03 option
Summary: Specialization of a function is not called if compile with -03 option
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-05 13:08 UTC by maxime fiandino
Modified: 2006-07-05 14:33 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail: 3.2.3
Last reconfirmed:


Attachments
The main, just call the template function foo and return the return value (65 bytes, application/octet-stream)
2006-07-05 13:11 UTC, maxime fiandino
Details
the specialization (77 bytes, application/octet-stream)
2006-07-05 13:12 UTC, maxime fiandino
Details
The header with the generic implementation (102 bytes, application/octet-stream)
2006-07-05 13:12 UTC, maxime fiandino
Details
The makefile to test (107 bytes, application/octet-stream)
2006-07-05 13:14 UTC, maxime fiandino
Details

Note You need to log in before you can comment on or make changes to this bug.
Description maxime fiandino 2006-07-05 13:08:06 UTC
The specialization of a function is not called if compile with -03 option,
the generic implementation is called insteed.
This appear only if the declaration of the specialization is in another
file.
The specialization is called if compiled with -02 or lees optimization, or
if the generic implementation is declared with the buildin noinline.

Configuration of gcc -v:

Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../src/configure --prefix=/project/spg/tools/Open-Sources/gcc/4.1.1/Linux-x86/ --enable-__cxa_atexit --enable-shared --enable-long-long --enable-threads --enable-languages=c,c++ --with-gnu-as --with-gnu-ar --with-gnu-ld --with-gcc
Thread model: posix
gcc version 4.1.1


Three files:

header


#ifndef _TCLASS_H_
#define _TCLASS_H_

template<typename T> bool foo(T val)
{
return false;
}
 
#endif 

scpecialization:
#include "tclass.h"


template<> bool foo(int val)
{
return true;
}

main:

#include "tclass.h"

int main() {

return(foo(1));

}
Comment 1 maxime fiandino 2006-07-05 13:11:05 UTC
Created attachment 11829 [details]
The main, just call the template function foo and return the return value
Comment 2 maxime fiandino 2006-07-05 13:12:07 UTC
Created attachment 11830 [details]
the specialization

The specialization on int return true
Comment 3 maxime fiandino 2006-07-05 13:12:58 UTC
Created attachment 11831 [details]
The header with the generic implementation

The generic template function return false
Comment 4 maxime fiandino 2006-07-05 13:14:28 UTC
Created attachment 11832 [details]
The makefile to test

This makefile create two executable

TC1 compiled with -02 which work well
TC2 compiled with -O3 which don't
Comment 5 maxime fiandino 2006-07-05 13:18:02 UTC
This bug was firstly discover under gcc3.2.3, then we test with 4.1.1 with the same results. The test case is very short.
Comment 6 Richard Biener 2006-07-05 14:04:46 UTC
You need to make the specialization visible at the point of instantiation.
Comment 7 maxime fiandino 2006-07-05 14:33:16 UTC
Thanks, for you quick answer Richard.

Where i don't understand is, it is working well if i declare
the generic template function with:

template<typename T> bool foo(T val) __attribute__(noinline)
{
return false;
}

Just in the case this workaround (perhaps evil) may interest some people.

I return to read the C++ spec this night, i missed this point.