By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Static Member of Template Class

P: n/a
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();
}

Jun 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jun 21 '06 #2

P: n/a

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

Jun 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.