我用C++&boost编写了一个程序。是否可以编写一个模板类,从具有未知数量参数的函数生成函子,例如 my_call<func>(vector<variant>)
,哪里可以bool fun(string)
乐趣或bool fun(int, int, string)
乐趣等等?
首先,重要的是要认识到boost::variant<>
是一个类模板,需要它可以容纳的所有可能类型的列表。所以,你不会只有一个vector<variant>
,而是一个vector<variant<string, double>>
或vector<variant<int, double, string, my_class>>
,你将无法混合它们。
这让我觉得你可能想使用boost::any
而不是boost::variant<>
。因此,我在这里提出了一个适用于boost::variant
的解决方案,并且可以稍微修改以使用 boost::any
,因此您可以选择您喜欢的版本。
首先,我必须
承认该解决方案易于使用,但不是那么容易理解,因此我必须首先介绍一些机制。这种机制对于基于变体和基于任何的解决方案都是通用的。
//=============================================================================
// META-FUNCTIONS FOR CREATING INDEX LISTS
// The structure that encapsulates index lists
template <size_t... Is>
struct index_list
{
};
// Collects internal details for generating index ranges [MIN, MAX)
namespace detail
{
// Declare primary template for index range builder
template <size_t MIN, size_t N, size_t... Is>
struct range_builder;
// Base step
template <size_t MIN, size_t... Is>
struct range_builder<MIN, MIN, Is...>
{
typedef index_list<Is...> type;
};
// Induction step
template <size_t MIN, size_t N, size_t... Is>
struct range_builder : public range_builder<MIN, N - 1, N - 1, Is...>
{
};
}
// Meta-function that returns a [MIN, MAX) index range
template<size_t MIN, size_t MAX>
using index_range = typename detail::range_builder<MIN, MAX>::type;
元类index_range
允许定义整数的编译时序列。乔纳森·韦克利(Jonathan Wakely)提出了一个有趣的建议,以标准化这种结构,这样就不需要整个机器了。但是,目前,我们必须按照上面完成的方式手动编码。
现在我们可以构建编译时整数序列,我们可以利用可变参数模板和参数解包来创建一种调度机制,将variant
参数的向量转换为常规参数列表。请注意,必须将具体的variant<>
类型作为模板参数提供。基于 any
的解决方案不需要这样做。
// Headers needed for the implementation of the dispatcher
#include <vector>
#include <functional>
#include <boost/variant.hpp>
// Just for convenience
using namespace std;
using boost::variant;
//============================================================================
// DISPATCHER IMPLEMENTATION
// Call dispatching mechanism: notice how the underlying variant type
// must be provided as a template argument (the first one)
template<typename VT, typename R, typename... Args>
struct dispatcher
{
template<typename F>
dispatcher(F f) : _f(f) { }
// The call operator which performs the variant dispatch
R operator () (vector<VT> const& v)
{
if (v.size() != sizeof...(Args))
{
// Wrong number of arguments provided!
return false;
}
// Delegates to internal function call: needed for deducing
// a sequence of integers to be used for unpacking.
index_range<0, sizeof...(Args)> indexes;
return do_call(v, indexes);
}
private:
// The heart of the dispatching mechanism
template<size_t... Is>
R do_call(vector<VT> const& v, index_list<Is...> indexes)
{
return _f((get_ith<Args>(v, Is))...);
}
// Helper function that extracts a typed value from the variant.
template<typename T>
T get_ith(vector<VT> const& v, size_t i)
{
return boost::get<T>(v[i]);
}
// Wrapper that holds the function to be invoked.
function<R(Args...)> _f;
};
// Helper function that allows deducing the input function signature
template<typename VT, typename R, typename... Args>
function<R (vector<VT> const&)> get_dispatcher(R (*f)(Args...))
{
dispatcher<VT, R, Args...> d(f);
return d;
}
最后,简要演示如何使用它。假设我们有两个测试函数,如下所示:
#include <iostream>
bool test1(string s, double d)
{
cout << s << " " << d << endl;
return true;
}
bool test2(int i1, int i2, string s1, string s2)
{
cout << i1 << " " << i2 << " " << s1 << " " << s2 << endl;
return true;
}
我们想要的是通过构建变体向量来调用它们,并将其调度到所需的函数。我必须再次强调这样一个事实,即我们需要指定我们的变体可以容纳的所有类型的列表。在这里,我假设这些类型是string
、double
和int
,但你的程序可能使用不同的类型。
此外,该解决方案基于实现类型擦除的std::function<>
,允许您创建不同类型的函子,但统一调用它们。因此,还提供了此std::function<>
的便利类型定义(这反过来取决于我们使用的variant<>
类型):
int main()
{
// A helper type definition for the variant
typedef variant<int, double, string> vt;
// A helper type definition for the function wrapper
typedef function<bool (vector<vt>)> dispatcher_type;
// Get a caller for the first function
dispatcher_type f1 = get_dispatcher<vt>(test1);
// Prepare arguments for the first function
vector<vt> v = {"hello", 3.14};
// Invoke the first function
f1(v);
// Get a caller for the second function
dispatcher_type f2 = get_dispatcher<vt>(test2);
// Prepare arguments for the second function
v.assign({1, 42, "hello", "world"});
// Invoke the second function
f2(v);
}
由于所有调度程序都有类型 dispatcher_type
,因此您可以轻松地将它们放入容器中。但是,您必须注意这样一个事实,即只有在运行时才会检测到使用错误参数数调用函数的尝试(在编译时不可能知道std::vector<>
包含多少个元素)。因此,必须采取适当的谨慎措施。
正如承诺的那样,我现在将稍微修改此解决方案以使用boost::any
而不是boost::variant
。优点是,由于boost::any
可以保存任何值,因此无需指定可用作函数参数的可能类型的列表。
当帮助程序机制保持不变时,必须按如下方式修改核心调度程序类模板:
#include <vector>
#include <functional>
#include <boost/any.hpp>
using namespace std;
using boost::any;
//=============================================================================
// DISPATCHER IMPLEMENTATION
template<typename R, typename... Args>
struct dispatcher
{
template<typename F>
dispatcher(F f) : _f(f) { }
// The call operator which performs the dispatch
R operator () (vector<any> const& v)
{
if (v.size() != sizeof...(Args))
{
// Wrong number of arguments provided!
return false;
}
// Delegates to internal function call: needed for deducing
// a sequence of integers to be used for unpacking.
index_range<0, sizeof...(Args)> indexes;
return do_call(v, indexes);
}
private:
// The heart of the dispatching mechanism
template<size_t... Is>
R do_call(vector<any> const& v, index_list<Is...> indexes)
{
return _f((get_ith<Args>(v, Is))...);
}
// Helper function that extracts a typed value from the variant.
template<typename T>
T get_ith(vector<any> const& v, size_t i)
{
return boost::any_cast<T>(v[i]);
}
// Wrapper that holds the function to be invoked.
function<R(Args...)> _f;
};
// Helper function
template<typename R, typename... Args>
function<R (vector<any> const&)> get_dispatcher(R (*f)(Args...))
{
dispatcher<R, Args...> d(f);
return d;
}
如您所见,VT
模板参数已消失。特别是,可以在不显式指定任何模板参数的情况下调用get_dispatcher
。使用我们为基于 variant
的解决方案定义的相同测试函数,下面介绍了如何调整main()
例程:
int main()
{
// Helper type definition
typedef function<bool (vector<any>)> dispatcher_type;
// Get a caller for the first function
dispatcher_type f1 = get_dispatcher(test1);
// Get a caller for the second function
dispatcher_type f2 = get_dispatcher(test2);
// Prepare arguments for the first function
vector<any> v = {string("hello"), 3.14};
// Invoke the first function
f1(v);
// Prepare arguments for the second function
v.assign({1, 42, string("hello"), string("world")});
// Invoke the second function
f2(v);
}
唯一的缺点是使用 boost::any
时不能显式分配字符串文字,因为字符串文字的类型是 char []
,数组不能用于初始化 any
类型的对象:
any a = "hello"; // ERROR!
因此,您必须将它们包装到string
对象中,或者将它们显式转换为指向char const*
的指针:
any a = string("hello"); // OK
any b = (char const*)"hello"; // OK
如果这对您来说不是一个大问题,那么最好采用第二种解决方案。