2

This code compiles with no headers, implying that size_t is intrinsically defined as unsigned long long:

int main()
{
    size_t i = -1;  // i is 18446744073709551615
}

But this Q&A says that std::size_t is defined in <cstddef> and may or may not be included in the global namespace.

However, I'm not including any headers, so where is this defined?

Further, if I defined it as a signed int, it also works! Note:
@user17732522 points out this is local so OK. When put in global it fails to compile.

int main()
{
    typedef long long int size_t;
    size_t i = -1;  // i is 1
}

Is this a bug, or some odd extension in Visual C++?

doug
  • 3,840
  • 1
  • 14
  • 18
  • It doesn't compile in GCC or clang. – interjay Feb 24 '22 at 22:43
  • @interjay Good to know. I've been using it widely w/o `std::` Probably gets put into global from headers in my programs. So looks like a bug. Likely to keep really old programs from breaking. Not likely to cause a problem I suppose. – doug Feb 24 '22 at 22:46
  • I suppose I could just put `typedef decltype(sizeof(int)) size_t;` in front of any code and it shouldn't cause any portability issues. Or include *cstddef* and add a using – doug Feb 24 '22 at 22:54
  • 1
    The second snippet declares a name `size_t` local to the function. It is not related to `::size_t` or `std::size_t`. – user17732522 Feb 24 '22 at 22:54
  • "*However, I'm not including any headers, so where is this defined?*" - it might be defined in a **Precompiled Header** that gets included in the compile via project settings rather than by code. – Remy Lebeau Feb 24 '22 at 22:56
  • @user17732522 Good point. In global if fails. That's good! – doug Feb 24 '22 at 22:57
  • @RemyLebeau It's not using precompiled headers. And nothing in the full option list shows anything weird. However, there is something called *vcruntime.h* that seems to define it. No idea how it's getting included. – doug Feb 24 '22 at 23:00
  • @doug VS does a lot behind the scenes for you, well hidden in Menus, inside menus, inside menus. It’s not shocking to me that they are either implicitly importing cstddef. Or have it defined intrinsic in the compiler – Taekahn Feb 24 '22 at 23:05
  • @Taekahn Yeah. I looked at *cstddef* and it's not doing it or including *vcruntime.h* seems it's (vcruntime) is just getting included because it's x64. – doug Feb 24 '22 at 23:15
  • 1
    Possible duplicate/related: [Which C++ compilers automatically define size\_t without requiring a header include?](https://stackoverflow.com/questions/29543394/which-c-compilers-automatically-define-size-t-without-requiring-a-header-inclu) – user17732522 Feb 24 '22 at 23:19
  • As far as I can tell this behavior is not standard-conform. It should put out a diagnostic that `size_t` is not declared, although the version declaring it in global scope probably has undefined behavior, because the name `::size_t` is reserved. – user17732522 Feb 24 '22 at 23:20
  • @user17732522 yep. Mike Seymour's answer is good. Thanks. Looks like something VC++ has been doing for a long time behind the scenes. Definitely a dup. – doug Feb 24 '22 at 23:24

0 Answers0