Lambdas in C ++ 03

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 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)); } 

Without a doubt, the Visual C ++ Studio 2008 Express SP1 compiler rejects lambda expressions, using boost will not help either, except that bind () and lambda boost got that I'm not quite sure how to do this with this.

To tell more about this, I also want to be able to use this:

 using namespace boost::assign; static std::vector<test> tests; static void init_tests(void) { push_back(tests) define_test(...) ...; } 

this means that structures with static functions will not matter much, for example:

 #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) ) 

This is because I am writing a ton of this, and C ++ 11 was so easy.

+5
source share
1 answer

For example, you can boil something with Boost Phoenix.

 s_tests.push_back(test ( "test1", 1, phx::val(1), phx::ref(v) = arg1*1 )); s_tests.push_back(test ( "test2", 2, phx::val(2), phx::ref(v) = arg1*2 )); 

It will not achieve the natural C ++ syntax, but at least it will be quite fully functional (it supports exceptions, while_, for_, switch_, locals, bind (), etc.):

Live on coliru

 #include <boost/function.hpp> struct test { const char *n; int i; boost::function<int(void)> read; boost::function<void(int)> write; test(char const* n, int i, boost::function<int(void)> read, boost::function<void(int)> write) : n(n), i(i), read(read), write(write) {} }; #include <boost/phoenix.hpp> #include <vector> using namespace boost::phoenix::arg_names; namespace phx = boost::phoenix; namespace mocks { static int v; typedef std::vector<test> test_t; test_t const& tests() { static test_t s_tests; if (s_tests.empty()) { s_tests.push_back(test ( "test1", 1, phx::val(1), phx::ref(v) = arg1*1 )); s_tests.push_back(test ( "test2", 2, phx::val(2), phx::ref(v) = arg1*2 )); } return s_tests; } } #include <iostream> int main() { for (mocks::test_t::const_iterator it = mocks::tests().begin(); it != mocks::tests().end(); ++it) { test const& test = *it; std::cout << "'" << test.n << "'\t" << test.i << ", " << test.read() << ", "; test.write(42); std::cout << "mock v: " << mocks::v << "\n"; } } 

Print

 'test1' 1, 1, mock v: 42 'test2' 2, 2, mock v: 84 
+4
source

Source: https://habr.com/ru/post/1212716/


All Articles