Re: local static data vs. global data
On 23.10.2012 10:36, Leslaw Bieniasz wrote:
In my program I need to have several (global) functions that share some
data, but I want to avoid introducing global data.
Either your design with global shared data is OK, then there is nothing
wrong with global data, or the design is bad. Then you should change
your design.
I am therefore trying
to use the following construct:
inline const long double *const get_data_ptr(void)
{
static const long double data[10]
{
1.0L, 2.0L, 3.0L, etc.
};
return data;
}
What are you trying to accomplish here?
Except for the overhead, I mean?
By the way, on many platforms this code has a race condition in
multi-threaded context because the initialization of local statics is
not implicitly thread-safe.
This compiles without errors, and seems to work
without errors. However, is this construct correct?
It depends on what you mean with correct. Yes, it is valid C++ code.
No, I would not do so.
If you are really talking only about constants, then feel free to define
a global constant.
If you do not want to expose this symbol in the global name space, then
place it in a separate namespace. Alternatively you can use a class,
that contains all the functions, that depend on your constants.
Marcel