Why doesn't ANSI C have namespaces?

前端 未结 9 1665
無奈伤痛
無奈伤痛 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

    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.

提交回复
热议问题