Does “std::size_t” make sense in C++?

前端 未结 8 594
Happy的楠姐
Happy的楠姐 2020-11-29 23:28

In some code I\'ve inherited, I see frequent use of size_t with the std namespace qualifier. For example:

std::size_t n = sizeof(          


        
8条回答
  •  离开以前
    2020-11-30 00:27

    The GNU compiler headers contain something like

    typedef long int __PTRDIFF_TYPE__;
    typedef unsigned long int __SIZE_TYPE__;

    Then stddef.h constains something like

    typedef __PTRDIFF_TYPE__ ptrdiff_t;
    typedef __SIZE_TYPE__ size_t;

    And finally the cstddef file contains something like

    #include 
    
    namespace std {
    
      using ::ptrdiff_t;
      using ::size_t;
    
    }
    

    I think that should make it clear. As long as you include you can use either size_t or std::size_t because size_t was typedefed outside the std namespace and was then included. Effectively you have

    typedef long int ptrdiff_t;
    typedef unsigned long int size_t;
    
    namespace std {
    
      using ::ptrdiff_t;
      using ::size_t;
    
    }
    

提交回复
热议问题