Re: mixed-sign arithmetic and auto
In article <478114A9.20209@erdani.org>,
"Andrei Alexandrescu (See Website For Email)"
<SeeWebsiteForEmail@erdani.org> wrote:
Nevin :-] Liber wrote:
auto x = static_cast<int>((i + u) / 2);
because they "know" that casts always "fix" these kinds of problems.
People tend to take the path of least resistance. In this case, I think
they'd write:
int c = (i + u) / 2;
which is shorter and does the same thing.
It means that while this compiles:
std::vector<X> v;
//...
auto middle_index_round_down = v.size() / 2;
the slightly different:
auto middle_index_round_up = (1 + v.size()) / 2;
would not (since it is (i + u) / 2).
Heck, even
if (!v.empty)
{
auto back_index = v.size() - 1;
// ...
}
would fail to compile.
In order to ensure that these types of things would compile, they would
have to go back to using std::vector<X>::size_type (or worse, they would
follow the path of least resistance and choose one of int, unsigned,
long, unsigned long or size_t). Isn't the whole point of auto so that
we don't have to use those kinds of expressions?
Heck, if I'm using std::bitset, I have to know that the type returned by
size() is size_t, since there isn't a typedef for size_type inside of it
to use. But I digress.
Nonono, that's certainly not what I had in mind. Again: in my opinion,
it might be useful to just disallow (or warn on) the use of auto in the
most flagrant cases of unsigned type mishandling.
I just don't know how to consistently tell at compile time which ones
are flagrant.
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> 773 961-1620
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]