Restrict to objects with static storage duration

V

Victor Bazarov

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
 
A

Abhishek Padmanabh

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

Victor Bazarov

Abhishek said:
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?

V
 
P

ppi

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
 
A

Abhishek Padmanabh

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

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

}

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

Victor Bazarov

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

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

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.

V
 
V

Victor Bazarov

Abhishek said:
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.

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
 
L

lbonafide

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?
 
V

Victor Bazarov

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

V
 
K

kasthurirangan.balaji

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

V

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

Thanks,
Balaji.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top