Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #4338

I have thought more about C++

From Sky89 <Sky89@sky68.com>
Newsgroups comp.programming.threads
Subject I have thought more about C++
Date 2018-05-25 10:16 -0400
Organization A noiseless patient Spider
Message-ID <pe95rl$asf$2@dont-email.me> (permalink)

Show all headers | 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


Thread

I have thought more about C++ Sky89 <Sky89@sky68.com> - 2018-05-25 10:16 -0400

csiph-web