Is there a way to restrict a class to only instantiate objects with
static storage duration, but no dynamic, and no automatic (hence no
function arguments, either) storage duration?
Disabling dynamic is easy (relatively), just make 'op new' private.
But automatic?
class AllowsOnlyStatics {
???
};
AllowsOnlyStatics OK;
class Other {
AllowsOnlyStatics notOK; // not sure about this one
};
int main() {
static AllowsOnlyStatics OKaswell;
AllowsOnlyStatics notOK; // should produce an error
Other o; // should also produce an error
}
Any ideas?
It's not something that I have to have, I probably can work around
it, but if it's available, I'd be interested to know. Thanks!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
How well it works for you, I am not sure. So, feel free to throw this
into the nearest garbage can. :-)
template<typename T, int i>
T& getStaticObject();
class OnlyStatic
{
private:
OnlyStatic(){std::cout << "ctor\n";}
OnlyStatic(const OnlyStatic&);
~OnlyStatic(){}
template<typename T, int i>
friend T& getStaticObject();
};
template<typename T, int i>
T& getStaticObject()
{
static T t;
return t;
}
int main()
{
OnlyStatic& osobj = getStaticObject<OnlyStatic,1>();
OnlyStatic& osobj2 = getStaticObject<OnlyStatic,2>();
}
There can be variants with getStaticObject as static member or a class
on its own. What I am trying to exploit is that each different
template instantiation would have its own copy of static data.
The problem, of course is that I have literally _hundreds_ of those
scattered throughout the code, so there is no way in hell I can keep
all those numbers unique manually.
Any other ideas?
well, the following is ugly but it can be quite practical ;-)
int main()
{
OnlyStatic& osobj = getStaticObject<OnlyStatic,__LINE__>();
OnlyStatic& osobj2 = getStaticObject<OnlyStatic,__LINE__>();
}
-- paulo
VS (.net 2002 onwards) and gcc (4.3) do have a predefined macro :
__COUNTER__. Don't know if some solution of this sort would be
available for your toolset. So, other than that, yes, it is painful/
unpractical. :-(
Yes, in _ONE_ particular module. I have _thousands_ of them. I am
not taking chances that my object definitions are always on different
lines in different translation units.
How is that predefined macro treated _across_ translation units?
And keep in mind that VS 2005+ has parallel compilation available.
> So, other than that, yes, it is painful/
> unpractical. :-(
V
Sorry I can't help, but I'm curious as to why one want to would do
this?
I am working on a caching mechanism for objects. To make things as
simple as possible I want to create those objects (and initialise them)
only once per program run. The simplest way I know is to create them
with static storage duration. At the same time the objects perform
expensive tasks when initialised, and especially if ever destroyed, so
I don't want anyone to create those objects with very short storage
duration (like automatic) or bother with dynamic memory (besides,
*that* is easy to prohibit).
Essentially, I have what looks like "numerous unnamed singletons".
They are so numerous that I don't want the overhead of creating them
dynamically, if I can help it (although it is generally possible to
restrict their creation to free store only). If no other solution is
eventually found, I'll turn to pooled memory manager and will make
those objects dynamically. This way I'll avoid the overhead...
may i suggest you to look at monostate pattern. you may also want to
customize it as per your need.
Thanks,
Balaji.