Skip to content

Instantly share code, notes, and snippets.

@IMelker
Last active August 18, 2020 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IMelker/a0ef2567eb9dd92715bd4ff5c5f7aa32 to your computer and use it in GitHub Desktop.
Save IMelker/a0ef2567eb9dd92715bd4ff5c5f7aa32 to your computer and use it in GitHub Desktop.
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