I go through the book "Accelerated C ++" by Andrew Koenig and Barbara E. Mu, and I have some questions about the main example in chapter 2. The code can be summarized as shown below and compiled without warning / error using g ++:
#include <string>
using std::string;
int main()
{
const string greeting = "Hello, world!";
const int pad = 1;
const string::size_type cols = greeting.size() + 2 + pad * 2;
string::size_type c = 0;
if (c == 1 + pad)
{;}
return 0;
}
However, if I replaced const int pad = 1;with int pad = 1;, the g ++ compiler will return a warning:
warning: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
if (c == 1 + pad)
If I replaced const int pad = 1;with unsigned int pad = 1;, the g ++ compiler will not return a warning.
I understand why g ++ returns a warning, but I'm not sure of the following three points:
- Can I use
unsigned intto compare with std::string::size_type? The compiler does not return a warning in this case, but I'm not sure if it is safe. const int pad = 1. pad unsigned int?const int pad = 1; string::size_type pad = 1;, pad . , , ?