sks wrote:
Hi,
Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
As others pointed out, the lengt parameter of substr is size_type,
unsigned.
But being often enough enoyed by the signed/unsigned warnings of
g++/vc++, I wondered why gcc didn't warn you.
Having installed a few versions, I tried:
$ g++-2.95.4 -Wall -O2 ref.cc -o ref && ./ref
This is a test string
$ g++-3.3.6 -Wall -O2 ref.cc -o ref && ./ref
ref.cc: In function `int main()':
ref.cc:8: warning: passing negative value `-1' for converting 2 of `
std::basic_string<_CharT, _Traits, _Allocstd::basic_string<_CharT,
_Traits, _Alloc>::substr(typename _Alloc::size_type, typename
_Alloc::size_type) const [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'
This is a test string
$ g++-4.0.3 -Wall -O2 ref.cc -o ref && ./ref
ref.cc: In function 'int main()':
ref.cc:8: warning: passing negative value '-0x00000000000000001' for
argument 2 to 'std::basic_string<_CharT, _Traits, _Alloc>
std::basic_string<_CharT, _Traits, _Alloc>::substr(typename
_Alloc::size_type, typename _Alloc::size_type) const [with _CharT =
char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
This is a test string
$ g++-4.1.1 -Wall -O2 ref.cc -o ref && ./ref
This is a test string
So the various g++ versions somtimes warn, sometimes don't. You just
happened to use a version that doesn't. Wonder why 4.1 stopped warning
again. (It's not in the .h files, as the cpp (-E) output generated by
4.0.3 doesn't generate a warning with 4.1.1, the reverse does warn)