eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.62k stars 758 forks source link

Mini refactoring: using std::size_t and std::numeric_limits #160

Closed knowledge4igor closed 7 years ago

knowledge4igor commented 7 years ago

I've done mini refactoring: 1) I replaced using size_t to std::size from C++11 standard; 2) Introduced using std::numeric_limits<std::size_t>::max() instead of castings -1.

Also asio::streambuf size method returns std::size(). I think it would be better to use std::size for consistency.

I will be glad if you consider this pull request.

eidheim commented 7 years ago

Thank you, however, I'm not 100% sure about replacing size_t with std::size_t. The way I see it, size_t is just a architecture dependent unsigned integer type, and since I would not write std::unsigned long I would also not write std::size_t. Also, size_t should never cause name collision, since it is such a common integer type and used in the POSIX standard libraries.

knowledge4igor commented 7 years ago

First of all, you can read about Difference between size_t and std::size_t. In my humble opinion, it would be better to use std::size_t in Modern C++, size_t is from C "era". Second, for consistency with asio::streambuf returning value (size() call returns std::size_t, you can see it in boost references). Third, std::numeric_limits<std::size_t>::max() looks better and more readable rather than static_cast<size_t>(-1). Finally, it's up to you. These changes are not serious, I just wanted to slightly improve code and make it better (cosmetic changes).

eidheim commented 7 years ago

According to https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_headers.html#manual.intro.using.headers.cheaders you are right:

The standard specifies that if one includes the C-style header ( in this case), the symbols will be available in the global namespace and perhaps in namespace std:: (but this is no longer a firm requirement.) On the other hand, including the C++-style header () guarantees that the entities will be found in namespace std and perhaps in the global namespace.

One note: you can probably remove #include <cstddef> since this header is already included in numerous other header files already.

knowledge4igor commented 7 years ago

You can probably remove #include since this header is already included in numerous other header files already.

It's a bad practice to do it. You can read about this topic here - Mistake # 6: Not explicitly including all STL headers required by your cpp code file. Ok, there are low chances that #include <cstddef> will be removed from some other headers, but in general it would be better explicitly put all need headers in cpp files.