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

前端 未结 8 566
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:00

    I think the clarifications are clear enough. The std::size_t makes good sense in C++ and ::size_t make (at least) good sense in C.

    However a question remain. Namely whether it is safe to assume that ::size_t and std::size_t are compatible?

    From a pure typesafe perspective they are not necessarily identical unless it is defined somewhere that they must be identical.

    I think many are using something a la:

    ----
    // a.hpp 
    #include <string>
    
    void Foo( const std::string & name, size_t value );
    
    -----
    // a.cpp
    #include "a.hpp"
    
    using namespace std;
    
    void Foo( const string & name, size_t value ) 
    {
      ...
    }
    

    So in the header you defintely use the ::size_t while in the source file you'll use std::size_t. So they must be compatible, right? Otherwise you'll get a compiler error.

    /Michael S.

    0 讨论(0)
  • 2020-11-30 00:06

    Sometimes other libraries will define their own size_t. For example boost. std::size_t specifies that you definitely want the c++ standard one.

    size_t is a c++ standard type and it is defined within the namespace std.

    0 讨论(0)
  • 2020-11-30 00:07

    You can get size_t in the global namespace by including, for example, <stddef.h> instead of <cstddef>. I can't see any obvious benefit, and the feature is deprecated.

    0 讨论(0)
  • 2020-11-30 00:08

    There seems to be confusion among the stackoverflow crowd concerning this

    ::size_t is defined in the backward compatibility header stddef.h . It's been part of ANSI/ISO C and ISO C++ since their very beginning. Every C++ implementation has to ship with stddef.h (compatibility) and cstddef where only the latter defines std::size_t and not necessarily ::size_t. See Annex D of the C++ Standard.

    0 讨论(0)
  • 2020-11-30 00:13

    Section 17.4.1.2 of the C++ standard, paragraph 4, states that:

    "In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std."

    This includes items found in headers of the pattern cname, including cstddef, which defines size_t.

    So std::size_t is in fact correct.

    0 讨论(0)
  • 2020-11-30 00:22

    size_t is not built into C++. And it is not defined by default. This one doesn't compile with GCC:

    int main(int argc, char** argv) {
    size_t size;
    }
    

    That said, size_t is part of POSIX and if you use only basic things like <cstdlib>, you will likely end up having it defined.

    You could argue that std::size_t is the C++ equivalent of size_t. As Brian pointed out, std:: is used as namespace to avoid setting global variables which don't fit everybody. It's just like std::string, which could also have been defined in the root namespace.

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