So, I am trying to replace the following code (C++11):
struct test {
const char *n;
int i;
std::function<int(void)> read;
std::function<void(int)> write;
};
#define define_test(n, i, bodyRead, bodyWrite) \
{ n, i, []() { bodyRead; }, [](int v) { bodyWrite; } }
std::initializer_list<test> tests = {
define_test("test1", 1, return 1, v = 2),
...
};
with a C++03 compatible code that gives the same effect:
struct test {
test(const char *_n, int _i, boost::function<int(void)> _read, boost::function<void(int)> _write) {
n = _n;
i = _i;
read = _read;
write = _write;
}
const char *n;
int i;
boost::function<int(void)> read;
boost::function<void(int)> write;
};
#define define_test(n, i, bodyRead, bodyWrite) \
( n, i, []() { bodyRead; }, [](int v) { bodyWrite; } )
std::vector<test> tests;
static void init_tests(void) {
tests.push_back(define_test("test1", 1, return 1, v = 2));
}
With no doubt, The Visual C++ studio 2008 Express compiler rejects the lambda expressions, using boost wouldn't help it either, except was bind() and the lambda boost got, which I am not exactly sure how I would do it with this.
To elaborate more on this, I want to also be able to use like so:
using namespace boost::assign;
static std::vector<test> tests;
static void init_tests(void) {
push_back(tests)
define_test(...)
...;
}
that means structs with static functions won't be of much help either e.g.:
#define define_test(n, i, bodyRead, bodyWrite) \
struct {
static void fn##n(void) { bodyRead; } \
static void fnw##n(int v) { bodyWrite; } \
}; \
( n, i, boost::bind(&fn##n), boost::bind(&fnw##n, boost::placeholders::_1) )
That's because I am writing a ton of this and C++11 was so easy.
Aucun commentaire:
Enregistrer un commentaire