Skip to content

Instantly share code, notes, and snippets.

@IMelker
Last active Aug 18, 2020
Embed
What would you like to do?
Cpp Tricks
// determining array size
template <typename T, auto N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
// convert defined value to string
#define expect(expr) if(!expr) cerr << "Assertion " << #expr \
" failed at " << __FILE__ << ":" << __LINE__ << endl;
#define stringify(x) #x
#define tostring(x) stringify(x)
#define MAGIC_CONSTANT 314159
cout << "Value of MAGIC_CONSTANT=" << tostring(MAGIC_CONSTANT);
// dump container content
#define dbg(v) copy(v.begin(), v.end(), ostream_iterator<typeof(*v.begin())>(cout, " "))
// debug for exact container
#ifdef NDEBUG
#define DEBUG(var)
#else
#define DEBUG(var) { std::cout << #var << ": " << (var) << std::endl; }
#endif
template <typename T1, typename T2>
std::ostream& operator<< (std::ostream& out, const std::map<T1,T2> &M) {
out << "{ ";
for (auto item:M) out << item.first << "->" << item.second << ", ";
out << "}";
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment