我正在修改here代码:
#include <boost/describe.hpp>
#include <boost/mp11.hpp>
#include <boost/json.hpp>
#include <boost/type_traits.hpp>
#include <boost/utility/string_view.hpp>
#include <stdexcept>
#include <string>
#include<iostream>
template<class C1, class C2, class R, class... A, std::size_t... I>
boost::json::value
call_impl_(C1& c1, R(C2::* pmf)(A...), boost::json::array const& args,
std::index_sequence<I...>)
{
return boost::json::value_from(
(c1.*pmf)(
boost::json::value_to< boost::remove_cv_ref_t<A> >(args[I])...));
}
template<class C1, class C2, class R, class... A>
boost::json::value
call_impl(C1& c1, R(C2::* pmf)(A...), boost::json::array const& args)
{
if (args.size() != sizeof...(A))
{
throw std::invalid_argument("Invalid number of arguments");
}
return call_impl_(c1, pmf, args, std::index_sequence_for<A...>());
}
template<class C>
boost::json::value
call(C& c, boost::string_view method, boost::json::value const& args)
{
using Fd = boost::describe::describe_members<C,
boost::describe::mod_public | boost::describe::mod_function>;
bool found = false;
boost::json::value result;
boost::mp11::mp_for_each<Fd>([&](auto D) {
if (!found && method == D.name)
{
result = call_impl(c, D.pointer, args.as_array());
found = true;
}
});
if (!found)
{
throw std::invalid_argument("Invalid method name");
}
return result;
}
struct Object
{
std::string greet(std::string const& who)
{
return "Hello, " + who + "!";
}
int add(int x, int y)
{
return x + y;
}
int foobar()
{
std::cout << "I'm stupid!" << std::endl;
return 1;
}
};
BOOST_DESCRIBE_STRUCT(Object, (), (greet, add, foobar))
int main()
{
Object obj;
std::cout << call(obj, "greet", { "world" }) << std::endl;
std::cout << call(obj, "add", { 1, 2 }) << std::endl;
boost::json::value sc{};
std::cout << call(obj, "add", sc) << std::endl;
}
我添加的部分是
int foobar()
{
std::cout << "I'm stupid!" << std::endl;
return 1;
}
和
boost::json::value sc{};
std::cout << call(obj, "foobar", sc) << std::endl;
输出:
error C2672: 'call_impl': no matching overloaded function found
I know I can add an overload of call_impl 和 modify call to:
boost::mp11::mp_for_each<Fd>([&](auto D) {
if (!found && method == D.name)
{
auto temp = args.as_array();
std::cout << typeid(temp).name() << std::endl;
if (!temp.empty())
result = call_impl(c, D.pointer,temp );
else
result = call_impl(c, D.pointer);
found = true;
}});
我的问题是:
- 如何构造包含空值的boost::json::value,以便我可以使用它调用该函数.
I think I've described my problem clearly, so many of my attempts 和 manipulations are so wrong that they don't need to be written, but it still warns me "It looks like your post is mostly code; please add some more details." ...
OS:windows11
IDE:visual studio2019