Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Access of a local class in a template function to a static variable

Reply
Thread Tools

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

 
 
ymost@hotmail.com
Guest
Posts: n/a
 
      02-03-2009
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?
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      02-03-2009
wrote:
> 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.

--
Michael
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      02-04-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template function within class template not not resolving when passedfunction local class? jrwats C++ 1 03-01-2010 08:30 AM
static class local variable in memeber function john C++ 5 08-12-2005 09:25 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57