Static Member of Template Class

Discussion in 'C++' started by Belebele, Jun 21, 2006.

  1. Belebele

    Belebele Guest

    Anything wrong with the code below? I cannot get g++ 2.96 to generate
    any code for Foo::Foo.

    Thanks in advance.

    class Foo {
    public:
    Foo(char const* literal) { cout << literal << endl; }
    };

    template <char const * const & literal>
    class Bar {
    public:
    static Foo foo_s;

    static void dummy() { }
    };

    template <char const * const & literal>
    Foo Bar<literal>::foo_s(literal);

    extern char const* LITERAL = "A LITERAL";

    void main()
    {
    Bar<LITERAL>::dummy();
    }
     
    Belebele, Jun 21, 2006
    #1
    1. Advertising

  2. Belebele wrote:
    > Anything wrong with the code below?


    'void main'

    > I cannot get g++ 2.96 to generate
    > any code for Foo::Foo.


    What do you mean? Does the program compile? Does it work as
    expected? What do you think it should do? What do you intend
    it to do? Why are you using such an old compiler?

    >
    > Thanks in advance.
    >
    > class Foo {
    > public:
    > Foo(char const* literal) { cout << literal << endl; }
    > };
    >
    > template <char const * const & literal>
    > class Bar {
    > public:
    > static Foo foo_s;
    >
    > static void dummy() { }
    > };
    >
    > template <char const * const & literal>
    > Foo Bar<literal>::foo_s(literal);
    >
    > extern char const* LITERAL = "A LITERAL";
    >
    > void main()
    > {
    > Bar<LITERAL>::dummy();
    > }


    A static object is not going to be instantiated unless it's
    explicitly referenced in the program. Your 'foo_s' isn't.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 21, 2006
    #2
    1. Advertising

  3. Belebele

    Belebele Guest

    Victor Bazarov wrote:
    > What do you mean? Does the program compile?


    Yes, the program compiled but, when put through a debugger, I could not
    get the single constructor of Foo to execute before main.

    > Does it work as expected? What do you think it should do? What do you intend
    > it to do?


    After you pointed out the fact that foo_s was not being used in the
    program, I realized that that was the cause of my problems. I threw in
    a reference to the object, and voila, it all works.

    I am trying to write code to detect the presence of required
    environment variables, used elsewhere in the app, at the beginning of
    the program execution. Programmers would not have to explicitly write
    code for the check, they would just simply use the name of the
    environment variable in their code as required, and the little
    framework would do the check automatically.

    > Why are you using such an old compiler?


    That is definitely not my call, you know ...

    Thanks for the prompt response

    Belebele
     
    Belebele, Jun 21, 2006
    #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. Simon G Best
    Replies:
    2
    Views:
    436
    Simon G Best
    Sep 25, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    817
    Micah Cowan
    Jan 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  4. Frederiek
    Replies:
    1
    Views:
    391
    Thomas Tutone
    Sep 14, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,363
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page