Re: Strange behaviour with container.
On 27 Des, 15:08, german diago <germandi...@gmail.com> wrote:
Hello. I'm trying to register some factory functions inside a
container which is static, like this one:
<code>
class Myclass {
static std::vector<...> funcs_;
static bool register_func(const std::string & classname,
boost::function<boost::any ()> f)
{ //put f inside funcs_; }
static boost::any create_instance(const std::string & classname);
};
</code>
Then, in my files I do like this (before main is started):
namespace {
const static bool value = MyClass::register_func("aclass",
create_instance<aclass>());
This create_instance call does not correspond to the declaration of
create_instance.
}
This should register things inside my funcs_ container, but when I use
create_instance to instantiate the type,
the container is empty!! Anyone knows a workaround?
At best we can only _guess_ at the problem, because you haven't
provided the real or sufficiently complete code.
A reasonable guess is that you're somehow running into the static
initialization order fiasco, which is discussed in the FAQ.
Another not unreasonable guess is that you're packaging the
initialization code in a library and your C++ implementation doesn't
handle that correctly (mostly this only applies to MSVC 6.0, and the
solution is then to upgrade to a less antique version).
The most likely guess is that you have a more direct bug.
Please see the FAQ item about how to ask a question about Code That
Does Not Work -- in particular, posting a complete small program is
very important, because since you don't know what's wrong chances are
that if you leave out anything then that's where the real problem is.
Cheers & hth.,
- Alf
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]