Why doesn't ANSI C have namespaces?

前端 未结 9 1632
無奈伤痛
無奈伤痛 2020-11-27 10:02

Having namespaces seems like no-brainer for most languages. But as far as I can tell, ANSI C doesn\'t support it. Why not? Any plans to include it in a future standard?

相关标签:
9条回答
  • 2020-11-27 10:32

    For completeness there are several ways to achieve the "benefits" you might get from namespaces, in C.

    One of my favorite methods is using a structure to house a bunch of method pointers which are the interface to your library/etc..

    You then use an extern instance of this structure which you initialize inside your library pointing to all your functions. This allows you to keep your names simple in your library without stepping on the clients namespace (other than the extern variable at global scope, 1 variable vs possibly hundreds of methods..)

    There is some additional maintenance involved but I feel that it is minimal.

    Here is an example:

    /* interface.h */
    
    struct library {
        const int some_value;
        void (*method1)(void);
        void (*method2)(int);
        /* ... */
    };
    
    extern const struct library Library;
    /* interface.h */
    
    /* interface.c */
    #include "interface.h"
    
    void method1(void)
    {
       ...
    }
    void method2(int arg)
    {
       ...
    }
    
    const struct library Library = {
        .method1 = method1,
        .method2 = method2,
        .some_value = 36
    };
    /* end interface.c */
    
    /* client code */
    #include "interface.h"
    
    int main(void)
    {
        Library.method1();
        Library.method2(5);
        printf("%d\n", Library.some_value);
        return 0;
    }
    /* end */
    

    The use of . syntax creates a strong association over the classic Library_function() Library_some_value method. There are some limitations however, for one you can't use macros as functions.

    0 讨论(0)
  • 2020-11-27 10:32

    Not an answer, but not a comment. C doesn't provide a way to define namespace explicitly. It has variable scope. For example:

    int i=10;
    
    struct ex {
      int i;
    }
    
    void foo() {
      int i=0;
    }
    
    void bar() {
      int i=5;
      foo();
      printf("my i=%d\n", i);
    }
    
    void foobar() {
      foo();
      bar();
      printf("my i=%d\n", i);
    }
    

    You can use qualified names for variables and functions:

    mylib.h
    
    void mylib_init();
    void mylib_sayhello();
    

    The only difference from namespaces it that you cannot be using and cannot import from mylib.

    0 讨论(0)
  • 2020-11-27 10:37

    C does have namespaces. One for structure tags, and one for other types. Consider the following definition:

    struct foo
    {
        int a;
    };
    
    typedef struct bar
    {
        int a;
    } foo;
    

    The first one has tag foo, and the later is made into type foo with a typedef. Still no name-clashing happens. This is because structure tags and types (built-in types and typedef'ed types) live in separate namespaces.

    What C doesn't allow is to create new namespace by will. C was standardized before this was deemed important in a language, and adding namespaces would also threaten backwards-compatibility, because it requires name mangling to work right. I think this can be attributed due to technicalities, not philosophy.

    EDIT: JeremyP fortunately corrected me and mentioned the namespaces I missed. There are namespaces for labels and for struct/union members as well.

    0 讨论(0)
提交回复
热议问题