Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces in C

Is there a way to (ab)use the C preprocessor to emulate namespaces in C?

I'm thinking something along these lines:

#define NAMESPACE name_of_ns some_function() {     some_other_function(); } 

This would get translated to:

name_of_ns_some_function() {     name_of_ns_some_other_function(); } 
like image 325
Kim Stebel Avatar asked Dec 23 '08 19:12

Kim Stebel


People also ask

What is namespace in C language?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

Why do we use namespace in C?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

What is namespace and example?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.


2 Answers

Another alternative would be to declare a struct to hold all your functions, and then define your functions statically. Then you'd only have to worry about name conflicts for the global name struct.

// foo.h #ifndef FOO_H #define FOO_H typedef struct {    int (* const bar)(int, char *);   void (* const baz)(void); } namespace_struct; extern namespace_struct const foo; #endif // FOO_H  // foo.c #include "foo.h" static int my_bar(int a, char * s) { /* ... */ } static void my_baz(void) { /* ... */ } namespace_struct const foo = { my_bar, my_baz }  // main.c #include <stdio.h> #include "foo.h" int main(void) {   foo.baz();   printf("%d", foo.bar(3, "hello"));   return 0; } 

In the above example, my_bar and my_baz can't be called directly from main.c, only through foo.

If you have a bunch of namespaces that declare functions with the same signatures, then you can standardize your namespace struct for that set, and choose which namespace to use at runtime.

// goo.h #ifndef GOO_H #define GOO_H #include "foo.h" extern namespace_struct const goo; #endif // GOO_H  // goo.c #include "goo.h" static int my_bar(int a, char * s) { /* ... */ } static void my_baz(void) { /* ... */ } namespace_struct const goo = { my_bar, my_baz };  // other_main.c #include <stdio.h> #include "foo.h" #include "goo.h" int main(int argc, char** argv) {   namespace_struct const * const xoo = (argc > 1 ? foo : goo);   xoo->baz();   printf("%d", xoo->bar(3, "hello"));   return 0; } 

The multiple definitions of my_bar and my_baz don't conflict, since they're defined statically, but the underlying functions are still accessible through the appropriate namespace struct.

like image 99
rampion Avatar answered Sep 22 '22 12:09

rampion


When using namespace prefixes, I normally add macros for the shortened names which can be activated via #define NAMESPACE_SHORT_NAMES before inclusion of the header. A header foobar.h might look like this:

// inclusion guard #ifndef FOOBAR_H_ #define FOOBAR_H_  // long names void foobar_some_func(int); void foobar_other_func();  // short names #ifdef FOOBAR_SHORT_NAMES #define some_func(...) foobar_some_func(__VA_ARGS__) #define other_func(...) foobar_other_func(__VA_ARGS__) #endif  #endif 

If I want to use short names in an including file, I'll do

#define FOOBAR_SHORT_NAMES #include "foobar.h" 

I find this a cleaner and more useful solution than using namespace macros as described by Vinko Vrsalovic (in the comments).

like image 40
Christoph Avatar answered Sep 19 '22 12:09

Christoph