C++ C+中的Lambdas+;03
因此,我尝试替换以下代码(C++11):C++ C+中的Lambdas+;03,c++,c++11,boost,lambda,c++03,C++,C++11,Boost,Lambda,C++03,因此,我尝试替换以下代码(C++11): 这是因为我写了很多这样的东西,C++11非常简单。例如,你可以用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 )); 它将无法实现自然的C++语法,但至少它将是非常完整的
这是因为我写了很多这样的东西,C++11非常简单。例如,你可以用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 ));
它将无法实现自然的C++语法,但至少它将是非常完整的(它支持异常,WHIELY,FARY,Switkl,Loopes,Band()等):
#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";
}
}
制作了示例和c++03证明。(当然,c++11中还有很多其他细节。)如果我的评论有点含糊,我很抱歉。假设我想保存一些输入,例如通过
#define define_test(n,I,bodyRead,bodyWrite)(n,I,boost::phoenix::val(bodyRead),boost::phoenix::ref(v)=boost::phoenix::arg_names::arg1;if_u(v)[bodyWrite;]
或者干脆[bodyWrite;]我不确定你想保存什么类型的输入。我认为你不能直接在你的梦想宏中包含“自然C++语句”(这是LAMBDAS的卖点)。.然而,在我看来,Boost Phoenix在这方面做得相当不错。我建议每次使用链接页面,或者重新考虑您的要求:)我担心,您是在寻求维护代码的便利性。(传递一个非平凡函数作为宏参数)@ diutl ucckcg-,我最终重新设计了这整个事情:(必须爱C++)
using namespace boost::assign;
static std::vector<test> tests;
static void init_tests(void) {
push_back(tests)
define_test(...)
...;
}
#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) )
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 ));
#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";
}
}
'test1' 1, 1, mock v: 42
'test2' 2, 2, mock v: 84