Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I have thought more about C++

23 views
Skip to first unread message

Sky89

unread,
May 25, 2018, 10:16:52 AM5/25/18
to
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.

0 new messages