What is the return type of sizeof operator?

前端 未结 3 1778
遥遥无期
遥遥无期 2020-12-18 17:31

What is the return type of sizeof operator? cppreference.com & msdn says sizeof returns size_t. Does it really return a size_t? I\'m using VS2010 Professional, and targ

3条回答
  •  遥遥无期
    2020-12-18 18:01

    C++11, §5.3.3 ¶6

    The result of sizeof and sizeof... is a constant of type std::size_t. [ Note: std::size_t is defined in the standard header (18.2). — end note ]

    You can also do a quick check:

    #include 
    #include 
    #include 
    
    int main()
    {
        std::cout<<(typeid(sizeof(int))==typeid(std::size_t))<

    which correctly outputs 1 on my machine.

    As @Adam D. Ruppe said in the comment, probably the compiler does not complain because, since it already knows the result, it knows that such "conversion" is not dangerous

提交回复
热议问题