Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can ssize_t be used in C++?

I am currently learning C++ and I saw there was a line of codes writing on the textbook:

ssize_t num = index; // index could be -1 in the example of my textbook`

The num is a variable to contain the index return from a traversing. However,after I copy this code to my C++ compiler, it says it can not find the definition of ssize_t. And I can hardly find there is any definitions about ssize_t in C++. I saw that it could be use in C but not C++. Right?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ssize_t can (and cannot) be used in C++ just as much as it can (and cannot) be used in C. There is no fundamental type by such name in either language, and there is no ssize_t type in the ISO C standard library, nor is there a std::ssize_t in the C++ standard library.

ssize_t is defined in the POSIX standard. If your program is compiled for a POSIX system, then you can use ssize_t by including the header from the POSIX library that defines it. On non POSIX system, such header doesn’t exist, so this is not portable. You shouldn’t use ssize_t in a cross platform program except in a POSIX specific module that uses the POSIX API.

When you aren’t using the POSIX API, you can typically substitute ssize_t with std::ptrdiff_t.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading