- Where does
size_tcome from when I don’t have anything included? - Is it reasonable to always assume
size_t==std::size_t? - When should I use the
size_typeinstdcontainers (string::size_type,vector<T>::size_type, etc)?
Where does size_t come from when I don’t have anything included? Is it reasonable
Share
If you don’t have anything included, then you can’t use
size_t. It’s defined in<stddef.h>(and perhaps also in<cstddef>, if your version of that header puts the definitions in the global namespace as well asstd).Yes. All types and functions defined by the C library are included in the
stdnamespace, as long as you include the appropriate C++ header (e.g.<cstddef>rather than<stddef.h>)Do you mean the
size_typetypes defined in some standard classes and templates such asvector? You could use those when using those classes if you like. In most cases, you’ll know that it’s the same assize_t, so you might as well use that and save a bit of typing. If you’re writing generic code, where you don’t know what the class is, then it’s better to usesize_typein case it’s not compatible withsize_t.For example, you might want to write a container designed to hold more items than can be represented by
size_t. You might use some kind of big number type to represent the container’s size, which isn’t convertible tosize_t. In that case, code likesize_t s = c.size()would fail to compile – you’d need to useContainer::size_typeinstead.