Skip to content

Instantly share code, notes, and snippets.

View kalman5's full-sized avatar

Gaetano kalman5

View GitHub Profile
template <class T>
class AutoPtr {
public:
AutoPtr(T* aPointer)
: thePointer(aPointer)
{}
~AutoPtr() {
delete thePointer;
}
int main() {
AutoPtr<Bomb> a(new Bomb());
a.reset();
}
@kalman5
kalman5 / AnotherReset
Created May 21, 2014 21:06
Another possible Reset implementation
void reset() {
T* tmp = thePointer;
thePointer = nullptr;
delete tmp;
}
#include <stdexcept>
class Bomb {
public:
~Bomb() { throw std::runtime_error("BOOM"); }
};
int main()
try {
Bomb b;
@kalman5
kalman5 / operator new
Created June 7, 2014 18:05
Operator new signatures
void* operator new (std::size_t size);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
void* operator new (std::size_t size, void* ptr) noexcept;
@kalman5
kalman5 / set_new_handler
Created June 20, 2014 19:51
set_new_handler
new_handler set_new_handler (new_handler new_p) noexcept;
@kalman5
kalman5 / ExampleNewHanlder
Last active August 29, 2015 14:02
Example set_new_handler
#include <iostream>
class ReservedMemory {
public:
ReservedMemory()
: theMemoryReserved(new char[80000*1024])
{}
~ReservedMemory() { delete []theMemoryReserved; }
void release() {
if (theMemoryReserved) {
@kalman5
kalman5 / SumScaleForInt
Created March 17, 2017 22:03
SumScaleForInt
int sumScale(int a, int b, int c) {
return c*(a + b);
}
@kalman5
kalman5 / SumScaleTemplate
Created March 17, 2017 22:04
SumScaleTemplate
template <class T>
T sumScale(T a, T b, int c) {
return c*(a + b);
}
@kalman5
kalman5 / SummableScalaleConcept
Created March 17, 2017 22:05
SummableScalaleConcept
template <class T>
concept bool SummableScalable() {
return requires(T a, T b, int c) {
{a + b}->T;
{c * a}->T;
};
}