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

Discussion in 'C++' started by ymost@hotmail.com, Feb 3, 2009.

  1. Guest

    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?
    , Feb 3, 2009
    #1
    1. Advertising

  2. 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
    Michael DOUBEZ, Feb 3, 2009
    #2
    1. Advertising

  3. James Kanze Guest

    Re: Access of a local class in a template function to a staticvariable

    On Feb 3, 2:36 pm, Michael DOUBEZ <> wrote:
    > 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.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.)

    --
    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
    James Kanze, Feb 4, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,816
    Christian Janßen
    Aug 8, 2003
  3. christopher diggins
    Replies:
    16
    Views:
    738
    Pete Becker
    May 4, 2005
  4. john
    Replies:
    5
    Views:
    277
    Victor Bazarov
    Aug 12, 2005
  5. jrwats
    Replies:
    1
    Views:
    884
    Anthony Williams
    Mar 1, 2010
Loading...

Share This Page