angelbeats's blog

By angelbeats, history, 3 months ago, In English
namespace std {
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define tee(...) [](const auto& x){ cerr << "[" << #__VA_ARGS__ << "]: "; debug_out(x); return x; }(__VA_ARGS__)
#else
#define dbg(...) do {} while(0)
#define tee(...) (__VA_ARGS__)
#endif
}

The code above introduces tee, which simply prints one value and returns it.

g++ -DLOCAL -O2 --std=c++17 -o a a.cpp

Don't use c++11. Don't change std to other names. You can get rid of the std wrapper.

And here's the complete version, which handles tuple of any length, as well as complex.

Complete version

namespace std { template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template<class Tuple, std::size_t... Is> string to_string_helper(const Tuple& t, std::index_sequence<Is...>) { return (((Is == 0? "(" : ", ") + to_string(std::get(t))) + ...) + ")"; } template<class... Args> string to_string(const std::tuple<Args...>& t) { return to_string_helper(t, std::index_sequence_for<Args...>{}); } template string to_string(const complex& c) { return "C" + to_string(make_pair(c.real(), c.imag())); } string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; }

template string to_string(bitset v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast('0' + v[i]); } return res; }

template string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; }

void debug_out() { cerr << endl; } template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }

ifdef LOCAL

define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)

define tee(...) [](const auto& x){ cerr << "[" << #__VA_ARGS__ << "]: "; debug_out(x); return x; }(__VA_ARGS__)

else

define dbg(...) do {} while(0)

define tee(...) (__VA_ARGS__)

endif

}

Read more »

 
 
 
 
  • Vote: I like it
  • -28
  • Vote: I do not like it

By angelbeats, 3 months ago, In English
portable version
#include <tuple>
using namespace std;
#define numargs(args...) tuple_size<decltype(make_tuple(args))>::value
#define argi(i, args...) get<i>(make_tuple(args))
#define rep(i, args...) for (int i = ((numargs(args) > 1) ? argi(0, args) : 0); i < argi(numargs(args) - 1, args); ++i)
#define repi(args...) rep(i, args)

First, let argv := make_tuple(args), to get argc out of args, we use argc := tuple_size<decltype(argv)>::value, according to link. Second, we use (argc - 1 > 0) ? get<1>(argv) : 0 as the base, and get<(argc - 1)>(argv) as the end.

The code below is tested among G++11, G++14, G++17, Clang++17. For all users including MSVC, use ... instead of args... and __VA_ARGS__ instead of args.

#include <tuple>
#include <cassert>
using namespace std;
#define numargs(args...) tuple_size<decltype(make_tuple(args))>::value
#define argi(i, args...) get<i>(make_tuple(args))
#define rep(i, args...) for (int i = ((numargs(args) > 1) ? argi(0, args) : 0); i < argi(numargs(args) - 1, args); ++i)
#define repi(args...) rep(i, args)

int main() {
    int s = 0;
    repi(5) s += i;
    rep(i, -4, 5) s += i;
    assert(s == 10);
    return 0;
}

By the way, I'm using #define pb(args...) push_back(args) instead of #define pb push_back

Read more »

 
 
 
 
  • Vote: I like it
  • +10
  • Vote: I do not like it