On Feb 3, 2:36 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
> ym...@hotmail.com wrote:
> > I have the following code which compiles and runs fine on
> > GCC 4.3.2:
> [snip]
> > But when I do the same thing with a templated type instead
> > of 'int', the code compiles but I get a link error:
> > //--------------------------------------------------------
> > template<typename T>
> > struct A {virtual T getnum()=0;};
> > template<typename T>
> > A<T>* func(T numin) {
> > static T num;
> > num=numin;
> > struct B : public A<T> {
> > T getnum(){
> > delete this;
> > return num; // <----- link error: undefined reference
> > to 'num'
> > }
> > };
> > return new B();
> > }
> > int main() {
> > func(3)->getnum();
> > return 0;
> > }
> > //--------------------------------------------------------
> > Is this a compiler bug, or is this actually not supposed to
> > work with templates?
> This is a compiler bug. AFAIK the program is well formed
> Comeau compiles it without problem.
Does it link as well? His problem only occured when linking.
According to the standard (at least as far as I can tell), it's
legal, and any conforming compiler should be able to handle it.
The basic rule (in §9.

says that "Declarations in a local
class can use only type names, static variables, extern
variables and functions, and enumerators from the enclosing
scope", and I can't find anything which would change this in a
template. (There are a few special rules for locally declared
names in templates, but either they apply only to class
templates, or they only concern the template parameters
themselves.)
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34