. , , .
[ , , - , . ]
, , :
:
#ifdef __cplusplus
#define static_forward(decl) namespace { extern decl; }
#define static_def(def) namespace { def; }
#else
#define static_forward(decl) static decl;
#define static_def(def) static def;
#endif
And we can do:
static_forward(struct foo foo_instance)
void some_function(void)
{
do_something_with(&foo_instance);
}
static_def(struct foo foo_instance = { 1, 2, 3 })
The C extension is simple, it looks like this:
static struct foo foo_instance;
void some_function(void)
{
do_something_with(&foo_instance);
}
static struct foo foo_instance = { 1, 2, 3 };
The C ++ extension is as follows:
namespace { extern struct foo foo_instance; }
void some_function(void)
{
do_something_with(&foo_instance);
}
namespace { struct foo foo_instance = { 1, 2, 3 }; }
So, in a nutshell, thanks to anonymous namespaces, C ++ actually does not have a static problem with a direct link, only the problem of its implementation with a method incompatible with C, which is a bridge with macros.
Several anonymous areas of the namespace in the same translation system are the same namespace, and the surrounding content area outside the namespace is visible.
source
share