M
Marcus Kwok
I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:
int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t). I get similar warnings when assigning the
return value of .size() to an int. However, even on Stroustrup's FAQ
<http://www.research.att.com/~bs/bs_faq2.html> he has:
for (int i = 0; i<v.size(); ++i)
which seems to me that it should be fine. In order to quiet the error,
I have casted the return value to an int:
if (i >= static_cast<int>(vec.size()))
Is this bad style? Is there a more elegant solution? I recall reading
somewhere that unsigned ints should be avoided due to implicit
conversion issues that may crop up or if the value happens to wrap
around (technically it should be undefined but I assume most
implementations will just wrap the value around), which is why I would
prefer not to use them. For example, in TC++PL:SE, section 16.3.4 has
an example of a weird issue (but he says the compiler will complain if
we are lucky):
Since a vector cannot have a negative number of elements, its size
must be non-negative. This is reflected in the requirement that
vector's size_type must be an unsigned type. This allows a greater
range of vector sizes on some architectures. However, it can also
lead to surprises:
void f(int i)
{
vector<char> vc0(-1); // fairly easy for compiler to warn against
vector<char> vc1(i);
}
void g()
{
f(-1); // trick f() into accepting a large positive number!
}
In the call f(-1), -1 is converted into a (rather large) postive
integer (Sec. C.6.3). If we are lucky, the compiler will find a way
of complaining.
--Bjarne Stroustrup, _The_C++_Programming_Language:_Special_Edition_
returned from std::vector.size() in code similar to the following:
int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t). I get similar warnings when assigning the
return value of .size() to an int. However, even on Stroustrup's FAQ
<http://www.research.att.com/~bs/bs_faq2.html> he has:
for (int i = 0; i<v.size(); ++i)
which seems to me that it should be fine. In order to quiet the error,
I have casted the return value to an int:
if (i >= static_cast<int>(vec.size()))
Is this bad style? Is there a more elegant solution? I recall reading
somewhere that unsigned ints should be avoided due to implicit
conversion issues that may crop up or if the value happens to wrap
around (technically it should be undefined but I assume most
implementations will just wrap the value around), which is why I would
prefer not to use them. For example, in TC++PL:SE, section 16.3.4 has
an example of a weird issue (but he says the compiler will complain if
we are lucky):
Since a vector cannot have a negative number of elements, its size
must be non-negative. This is reflected in the requirement that
vector's size_type must be an unsigned type. This allows a greater
range of vector sizes on some architectures. However, it can also
lead to surprises:
void f(int i)
{
vector<char> vc0(-1); // fairly easy for compiler to warn against
vector<char> vc1(i);
}
void g()
{
f(-1); // trick f() into accepting a large positive number!
}
In the call f(-1), -1 is converted into a (rather large) postive
integer (Sec. C.6.3). If we are lucky, the compiler will find a way
of complaining.
--Bjarne Stroustrup, _The_C++_Programming_Language:_Special_Edition_