Sujet : Re: size_t best practice
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 18. Aug 2024, 22:57:23
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86ed6lebp8.fsf@linuxsc.com>
References : 1 2
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Mark Summerfield <mark@qtrac.eu> wrote or quoted:
>
So is it considered best practice to use int, long, long long, or size_t,
in situations like these?
>
In *cough*C++*cough* you could whip up a "SafeSize" class with
a bulletproof "operator--", so you don't space on the check.
You could still keep cranking out your code in what's basically C and
just cherry-pick this one gnarly feature from that other language.
>
SafeSize& operator--()
{ if( value == 0 )
{ throw std::underflow_error("SafeSize decrement underflow"); }
--value;
return *this; }
Besides this facility not being a solution to the underlying
problem, C++ has diverged from C to the point where it is
no longer possible to program in C++ as "basically C".