Static Member of Template Class

B

Belebele

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();
}
 
V

Victor Bazarov

Belebele said:
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
 
B

Belebele

Victor said:
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
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top