Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Restrict to objects with static storage duration

0 views
Skip to first unread message

Victor Bazarov

unread,
Mar 27, 2008, 10:09:27 AM3/27/08
to
I am drawing a blank here...

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


Abhishek Padmanabh

unread,
Mar 27, 2008, 10:49:00 AM3/27/08
to
On Mar 27, 7:09 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> I am drawing a blank here...
>
> 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?

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.

Victor Bazarov

unread,
Mar 27, 2008, 11:10:08 AM3/27/08
to

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?

ppi

unread,
Mar 27, 2008, 11:38:02 AM3/27/08
to

well, the following is ugly but it can be quite practical ;-)

int main()
{
OnlyStatic& osobj = getStaticObject<OnlyStatic,__LINE__>();
OnlyStatic& osobj2 = getStaticObject<OnlyStatic,__LINE__>();
}

-- paulo

Abhishek Padmanabh

unread,
Mar 27, 2008, 12:13:29 PM3/27/08
to

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. :-(

Victor Bazarov

unread,
Mar 27, 2008, 12:58:01 PM3/27/08
to

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.

Victor Bazarov

unread,
Mar 27, 2008, 12:59:27 PM3/27/08
to

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

lbon...@yahoo.com

unread,
Mar 27, 2008, 1:05:49 PM3/27/08
to
On Mar 27, 9:09 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> I am drawing a blank here...
>
> 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?

Sorry I can't help, but I'm curious as to why one want to would do
this?

Victor Bazarov

unread,
Mar 27, 2008, 2:11:36 PM3/27/08
to

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...

kasthurira...@gmail.com

unread,
Mar 28, 2008, 1:23:00 AM3/28/08
to
On Mar 27, 11:11 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

may i suggest you to look at monostate pattern. you may also want to
customize it as per your need.

Thanks,
Balaji.

0 new messages