Skip to content

Commit

Permalink
Exception example
Browse files Browse the repository at this point in the history
  • Loading branch information
knebekaizer committed Apr 8, 2022
1 parent 3ac2685 commit bd11b54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/vector_size_sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ add_executable(fancy_ptr
fancy_ptr.h
)

add_executable(fancy_ptr_trace
fancy_ptr.cpp
fancy_ptr.h
stacktrace.cpp
)

8 changes: 7 additions & 1 deletion examples/vector_size_sample/fancy_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@

#include "fancy_ptr.h"

#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

extern "C" int foo([[maybe_unused]] const int* buf, [[maybe_unused]] unsigned long n) {
return 0;
}
Expand All @@ -59,10 +62,13 @@ void foo_at_lvalue(FancyVec& v, size_t n) {
v.at(n) = 42;
}

void test() {
void test() try {
FancyVec v(10);
v.insert(v.end(), 42);
assert(v.size() == 11);
} catch (std::exception& e) {
cerr << "[ERROR] terminating with exception:\n" << e.what() << endl;
// throw;
}

// Explicit instantiation
Expand Down
13 changes: 12 additions & 1 deletion examples/vector_size_sample/fancy_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@
using std::nullptr_t;
using std::true_type;

#include <string>
#include <exception>
std::string Backtrace(int skip = 1);

namespace my {

class Error : public std::runtime_error {
public:
Error(const std::string& s) : std::runtime_error(s) {}
};

inline void throw_my() { throw Error(Backtrace(2)); }

template<class Tp> class Ptr;
template<class Tp> class CPtr;
Expand Down Expand Up @@ -114,7 +124,7 @@ struct CPtr {
CPtr(nullptr_t) : p_(nullptr) {}
CPtr(const T* p) : p_(p) {}

CPtr(const Ptr<T>& p) : CPtr(p.p_) {}
CPtr(const Ptr<T>& p) : p_(p.p_) { throw_my(); }

CPtr(const CPtr<T> &) = default;
CPtr(CPtr<T> &&) = default;
Expand Down Expand Up @@ -178,6 +188,7 @@ ptrdiff_t operator-(const Ptr<T>& a, const Ptr<T> b) {
return a.p_ - b.p_;
}


template<class Tp>
class custom_allocator;

Expand Down

0 comments on commit bd11b54

Please sign in to comment.