Friday, November 19, 2021

Digest for comp.lang.c++@googlegroups.com - 2 updates in 1 topic

Bonita Montero <Bonita.Montero@gmail.com>: Nov 19 06:08AM +0100

With C and C++ the compiler can assume that signed integer operations
never overflow. So I wrote a little C++20-class called sgi<T> with which
you can have overflows and you can check for them, f.e. with a + b < a.
 
#include <iostream>
#include <random>
#include "sgi.h"
 
using namespace std;
 
short rnd();
 
int main()
{
using sgis = sgi<short>;
sgis a( rnd() ), b( rnd() );
bool f = a + b < a;
cout << f << endl;
}
 
#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute((noinline))

No comments: