Skip to content

Instantly share code, notes, and snippets.

@ognis1205
Last active November 28, 2020 03:12
Show Gist options
  • Save ognis1205/653514ab09cb3baaf5bc54d719fcf6a2 to your computer and use it in GitHub Desktop.
Save ognis1205/653514ab09cb3baaf5bc54d719fcf6a2 to your computer and use it in GitHub Desktop.
Simple Debugging Macros for C++11.
#include <iostream>
#include <set>
#include <vector>
using namespace std;
/* IO Helper Functions. */
struct has_begin_end_empty {
template<typename T>
static auto check(T&& t) -> decltype(t.begin(), t.end(), t.empty(), true_type {});
template<typename T>
static auto check(...) -> false_type;
};
template<typename T>
struct is_container : public decltype(has_begin_end_empty::check<T>(declval<T>())) {};
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) noexcept;
template<typename... Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) noexcept;
template<typename C,
typename enable_if<is_container<C>::value && !is_same<C, string>::value, nullptr_t>::type=nullptr>
ostream& operator<<(ostream& os, const C& cont) noexcept {
os << "[";
for (auto it = begin(cont); it != end(cont); it++) os << *it << (next(it) != end(cont) ? ", " : "");
return os << "]";
}
template<typename T,
size_t N,
typename enable_if<!is_same<T, char>::value, nullptr_t>::type=nullptr>
ostream& operator<<(ostream& os, const T (&arr)[N]) noexcept {
os << "[";
for (auto it = begin(arr); it != end(arr); it++) os << *it << (next(it) != end(arr) ? ", " : "");
return os << "]";
}
template<size_t Index=0, typename... Ts>
inline typename enable_if<Index == sizeof...(Ts), void>::type
PrintTuple(ostream& os, const tuple<Ts...>& t) noexcept {}
template<size_t Index=0, typename... Ts>
inline typename enable_if<Index < sizeof...(Ts), void>::type
PrintTuple(ostream& os, const tuple<Ts...>& t) noexcept {
os << get<Index>(t) << (Index == sizeof...(Ts) - 1 ? "" : ", ");
PrintTuple<Index + 1, Ts...>(os, t);
}
template<typename... Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) noexcept {
os << "("; PrintTuple(os, t); return os << ")";
}
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) noexcept {
return os << "(" << p.first << ", " << p.second << ")";
}
/* Macros of Simple Debugging Operations. */
#ifdef LOCAL
void Debug() {}
template<typename Head, typename... Tail>
void Debug(Head& h, Tail&&... ts) {
const auto Size = sizeof...(Tail);
cerr << h << (Size ? ", " : "");
Debug(forward<Tail>(ts)...);
}
# define DBG(...) do { cerr << "\u001b[36m[DEBUG]\u001b[0m\t" << #__VA_ARGS__ << ": "; Debug(__VA_ARGS__); cerr << endl; } while (0)
#else
# define VER(...)
# define DBG(...)
#endif
/* Main Function. */
int main(int argc, char* argv[]) {
set<vector<string>> a{{{"abc", "def"},{"ghi"}}, {{"klm","nop"},{"qrs"}}, {{"tuv", "wxy", "zab"}}};
pair<pair<double, unsigned int>, pair<int, string>> b{{234.34534, 42}, {133, "IOI"}};
DBG(a, b);
return 0;
}
@ognis1205
Copy link
Author

Note: This template uses some c++11 functions, so you have to compile it with c++11 flag.
Example:- $ g++ -DLOCAL -std=c++11 simple_debug.cc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment