Access of a local class in a template function to a static variable

Y

ymost

Hi,
I have the following code which compiles and runs fine on GCC 4.3.2:

//--------------------------------------------------------
struct A {virtual int getnum()=0;};

A* func(int numin) {
static int num;
num=numin;
struct B : public A {
int getnum(){
delete this;
return num;
}
};
return new B();
}

int main() {
func(3)->getnum();
return 0;
}
//--------------------------------------------------------

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?
 
M

Michael DOUBEZ

Hi,
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.
 
J

James Kanze

(e-mail address removed) wrote:
I have the following code which compiles and runs fine on
GCC 4.3.2:
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.8) 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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top