Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #4338
| Path | csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Sky89 <Sky89@sky68.com> |
| Newsgroups | comp.programming.threads |
| Subject | I have thought more about C++ |
| Date | Fri, 25 May 2018 10:16:20 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 102 |
| Message-ID | <pe95rl$asf$2@dont-email.me> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Fri, 25 May 2018 14:16:21 -0000 (UTC) |
| Injection-Info | h2725194.stratoserver.net; posting-host="08aac1682740a10841138cc23ecea9e0"; logging-data="11151"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ozMdSMdDJy9/l7h8p1nge" |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 |
| Cancel-Lock | sha1:mEUsJr8xt/sSRj4/jApOQUYR/5Y= |
| Content-Language | en-US |
| X-Mozilla-News-Host | news://news.eternal-september.org:119 |
| Xref | csiph.com comp.programming.threads:4338 |
Show key headers only | View raw
Hello..
I have thought more about C++, and I think C++ is really powerful
because STL vectors perform bounds checking when the .at() member
function is called, but do not perform any checks on the [] operator
when out of bounds, the [] operator produces undefined results,
and for integer overflow or more efficient strict-type safety, here
is how to do it with SafeInt here:
https://github.com/dcleblanc/SafeInt
I have just written the following program using SafeInt, please look at
it and try it to notice that C++ is powerful:
===
#include "SafeInt.hpp"
using namespace std;
#include <climits>
#include <iostream>
#include <sstream>
#include <stdexcept>
class my_exception : public std::runtime_error {
std::string msg;
public:
my_exception(const std::string &arg, const char *file, int line) :
std::runtime_error(arg) {
std::ostringstream o;
o << file << ":" << line << ": " << arg;
msg = o.str();
}
~my_exception() throw() {}
const char *what() const throw() {
return msg.c_str();
}
};
#define throw_line(arg) throw my_exception(arg, __FILE__, \
__LINE__);
class CMySafeIntException : public SafeIntException
{
public:
static void SafeIntOnOverflow()
{
cout << "Caught a SafeInt Overflow exception!" << endl;
throw_line("SafeInt exception");
}
static void SafeIntOnDivZero()
{
cout << "Caught a SafeInt Divide By Zero exception!" << endl;
throw_line("SafeInt exception");
}
};
void a1(SafeInt<unsigned __int8, CMySafeIntException> a)
{
cout << (int)a << endl;
}
int main()
{
try {
//throw std::invalid_argument("exception");
unsigned __int8 i1 = 250;
unsigned __int8 i2 = 150;
SafeInt<unsigned __int8, CMySafeIntException> si1(i1);
SafeInt<unsigned __int8, CMySafeIntException> si2(i2);
SafeInt<unsigned __int8, CMySafeIntException> siResult = si1 + si2;
cout << (int)siResult << endl;
a1(-1);
}
catch (const std::runtime_error &ex) {
std::cout << ex.what() << std::endl;
}
//catch (const std::invalid_argument &ex) {
// std::cout << ex.what() << std::endl;
// }
}
====
So as you have nmoticed C++ is great !
Thank you,
Amine Moulay Ramdane.
Back to comp.programming.threads | Previous | Next | Find similar | Unroll thread
I have thought more about C++ Sky89 <Sky89@sky68.com> - 2018-05-25 10:16 -0400
csiph-web