C++ Boost Parameter超详细讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++ Boost Parameter超详细讲解

无水先生   2022-11-25 我要评论

一、说明

Boost.Parameter 使得将参数作为键/值对传递成为可能。除了支持函数参数外,该库还支持模板参数。 Boost.Parameter 在您使用长参数列表并且参数的顺序和含义难以记住时特别有用。键/值对使得以任何顺序传递参数成为可能。因为每一个值都是通过一个键来传递的,所以各种值的含义也更加清晰。

二、示例代码

示例 53.1。作为键/值对的函数参数

#include <boost/parameter.hpp>
#include <string>
#include <iostream>
#include <ios>
BOOST_PARAMETER_NAME(a)
BOOST_PARAMETER_NAME(b)
BOOST_PARAMETER_NAME(c)
BOOST_PARAMETER_NAME(d)
BOOST_PARAMETER_NAME(e)
BOOST_PARAMETER_FUNCTION(
  (void),
  complicated,
  tag,
  (required
    (a, (int))
    (b, (char))
    (c, (double))
    (d, (std::string))
    (e, *)
  )
)
{
  std::cout.setf(std::ios::boolalpha);
  std::cout << a << '\n';
  std::cout << b << '\n';
  std::cout << c << '\n';
  std::cout << d << '\n';
  std::cout << e << '\n';
}
int main()
{
  complicated(_c = 3.14, _a = 1, _d = "Boost", _b = 'B', _e = true);
}

Example53.1

示例 53.1 定义了一个函数 complicated(),它需要五个参数。参数可以按任何顺序传递。 Boost.Parameter 提供了宏 BOOST_PARAMETER_FUNCTION 来定义这样的函数。

在可以使用 BOOST_PARAMETER_FUNCTION 之前,必须定义键/值对的参数。这是通过宏 BOOST_PARAMETER_NAME 完成的,它只是传递了一个参数名称。该示例使用 BOOST_PARAMETER_NAME 五次来定义参数名称 a、b、c、d 和 e。

请注意,参数名称是在命名空间标记中自动定义的。这应该避免与程序中的同名定义发生冲突。

定义参数名称后,BOOST_PARAMETER_FUNCTION 用于定义函数 complicated()。传递给 BOOST_PARAMETER_FUNCTION 的第一个参数是返回值的类型。这在示例中是无效的。请注意,类型必须用括号括起来——第一个参数是 (void)。

第二个参数是正在定义的函数的名称。第三个参数是包含参数名称的名称空间。在第四个参数中,访问参数名称以进一步指定它们。

在示例 53.1 中,第四个参数以 required 开头,这是一个使后面的参数成为必需的关键字。 required 后跟一对或多对,由参数名称和类型组成。将类型括在括号中很重要。

各种类型用于参数 a、b、c 和 d。例如,a 可用于将 int 值传递给 complicated()。没有为 e 给出类型。相反,使用星号,这意味着传递的值可以具有任何类型。 e 是一个模板参数。

将各种参数传递给 BOOST_PARAMETER_FUNCTION 后,定义函数体。像往常一样,这是在一对大括号之间完成的。可以在函数体中访问参数。它们可以像变量一样使用,在 BOOST_PARAMETER_FUNCTION 中分配类型。示例 53.1 将参数写入标准输出。

complicated() 是从 main() 调用的。参数以任意顺序传递给 complicated()。参数名称以下划线开头。 Boost.Parameter 使用下划线来避免与其他变量名称冲突。

注意:

要在 C++ 中将函数参数作为键/值对传递,您还可以使用命名参数习惯用法,它不需要像 Boost.Parameter 这样的库。

示例 53.2。可选功能参数

#include <boost/parameter.hpp>
#include <string>
#include <iostream>
#include <ios>
BOOST_PARAMETER_NAME(a)
BOOST_PARAMETER_NAME(b)
BOOST_PARAMETER_NAME(c)
BOOST_PARAMETER_NAME(d)
BOOST_PARAMETER_NAME(e)
BOOST_PARAMETER_FUNCTION(
  (void),
  complicated,
  tag,
  (required
    (a, (int))
    (b, (char)))
  (optional
    (c, (double), 3.14)
    (d, (std::string), "Boost")
    (e, *, true))
)
{
  std::cout.setf(std::ios::boolalpha);
  std::cout << a << '\n';
  std::cout << b << '\n';
  std::cout << c << '\n';
  std::cout << d << '\n';
  std::cout << e << '\n';
}
int main()
{
  complicated(_b = 'B', _a = 1);
}

BOOST_PARAMETER_FUNCTION 还支持定义可选参数。

在示例 53.2 中,参数 c、d 和 e 是可选的。这些参数使用可选关键字在 BOOST_PARAMETER_FUNCTION 中定义。

可选参数的定义类似于必需参数:参数名称后跟类型。像往常一样,类型被括在括号中。但是,可选参数需要有默认值。

通过调用 complicated(),仅传递参数 a 和 b。这些是唯一需要的参数。由于未使用参数 c、d 和 e,因此将它们设置为默认值。

除了 BOOST_PARAMETER_FUNCTION 之外,Boost.Parameter 还提供宏。例如,您可以使用 BOOST_PARAMETER_MEMBER_FUNCTION 来定义成员函数,并使用 BOOST_PARAMETER_CONST_MEMBER_FUNCTION 来定义常量成员函数。

您可以使用 Boost.Parameter 定义函数,尝试自动为参数赋值。在这种情况下,您不需要传递键/值对——只传递值就足够了。如果所有值的类型不同,Boost.Parameter 可以检测出哪个值属于哪个参数。这可能需要您对模板元编程有更深入的了解。

示例 53.3。作为键/值对的模板参数

#include <boost/parameter.hpp>
#include <boost/mpl/placeholders.hpp>
#include <type_traits>
#include <typeinfo>
#include <iostream>
BOOST_PARAMETER_TEMPLATE_KEYWORD(integral_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(floating_point_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(any_type)
using namespace boost::parameter;
using boost::mpl::placeholders::_;
typedef parameters<
  required<tag::integral_type, std::is_integral<_>>,
  required<tag::floating_point_type, std::is_floating_point<_>>,
  required<tag::any_type, std::is_object<_>>
> complicated_signature;
template <class A, class B, class C>
class complicated
{
public:
  typedef typename complicated_signature::bind<A, B, C>::type args;
  typedef typename value_type<args, tag::integral_type>::type integral_type;
  typedef typename value_type<args, tag::floating_point_type>::type
    floating_point_type;
  typedef typename value_type<args, tag::any_type>::type any_type;
};
int main()
{
  typedef complicated<floating_point_type<double>, integral_type<int>,
    any_type<bool>> c;
  std::cout << typeid(c::integral_type).name() << '\n';
  std::cout << typeid(c::floating_point_type).name() << '\n';
  std::cout << typeid(c::any_type).name() << '\n';
}

Example53.3

示例 53.3 使用 Boost.Parameter 将模板参数作为键/值对传递。与函数一样,可以按任何顺序传递模板参数。

该示例定义了一个类 complicated,它需要三个模板参数。因为参数的顺序无关紧要,所以它们称为 A、B 和 C。A、B 和 C 不是访问类模板时将使用的参数名称。与函数一样,参数名称是使用宏定义的。对于模板参数,使用 BOOST_PARAMETER_TEMPLATE_KEYWORD。示例 53.3 定义了三个参数名称 integral_type、floating_point_type 和 any_type。

定义参数名称后,您必须指定可以传递的类型。例如,参数 integral_type 可用于传递 int 或 long 等类型,但不能传递 std::string 等类型。 boost::parameter::parameters 用于创建引用参数名称的签名,并定义可以与每个参数一起传递的类型。

boost::parameter::parameters 是一个描述参数的元组。必需参数标有 boost::parameter::required。

boost::parameter::required 需要两个参数。第一个是使用 BOOST_PARAMETER_TEMPLATE_KEYWORD 定义的参数名称。第二个标识参数可能设置的类型。例如,integral_type 可以设置为整数类型。此要求用 std::is_integral<_> 表示。 std::is_integral<_> 是一个基于 Boost.MPL 的 lambda 函数。 boost::mpl::placeholders::_ 是这个库提供的占位符。如果将设置了 integral_type 的类型传递给 std::is_integral 而不是 boost::mpl::placeholders::_,并且结果为真,则使用有效类型。其他参数 floating_point_type 和 any_type 的要求以类似方式定义。

创建签名并将其定义为 complicated_signature 后,复杂类将使用它。首先,使用 complicated_signature::bind 将签名绑定到模板参数 A、B 和 C。新类型 args 表示传递的模板参数与模板参数必须满足的要求之间的联系。接下来,访问 args 以获取参数值。这是通过 boost::parameter::value_type 完成的。 boost::parameter::value_type 期望参数和要传递的参数。该参数确定创建的类型。在示例 53.3 中,类 complicated 中的类型定义 integral_type 用于获取通过参数 integral_type 传递给 complicated 的类型。

main() 访问复杂的实例化类。参数 integral_type 设置为 int,floating_point_type 设置为 double,any_type 设置为 bool。传递的参数的顺序无关紧要。然后通过 typeid 访问类型定义 integral_type、floating_point_type 和 any_type 以获取它们的基础类型。该示例使用 Visual C++ 2013 编译,将 int、double 和 bool 写入标准输出。

示例 53.4。可选模板参数

#include <boost/parameter.hpp>
#include <boost/mpl/placeholders.hpp>
#include <type_traits>
#include <typeinfo>
#include <iostream>
BOOST_PARAMETER_TEMPLATE_KEYWORD(integral_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(floating_point_type)
BOOST_PARAMETER_TEMPLATE_KEYWORD(any_type)
using namespace boost::parameter;
using boost::mpl::placeholders::_;
typedef parameters<
  required<tag::integral_type, std::is_integral<_>>,
  optional<tag::floating_point_type, std::is_floating_point<_>>,
  optional<tag::any_type, std::is_object<_>>
> complicated_signature;
template <class A, class B = void_, class C = void_>
class complicated
{
public:
  typedef typename complicated_signature::bind<A, B, C>::type args;
  typedef typename value_type<args, tag::integral_type>::type integral_type;
  typedef typename value_type<args, tag::floating_point_type, float>::type
    floating_point_type;
  typedef typename value_type<args, tag::any_type, bool>::type any_type;
};
int main()
{
  typedef complicated<floating_point_type<double>, integral_type<short>> c;
  std::cout << typeid(c::integral_type).name() << '\n';
  std::cout << typeid(c::floating_point_type).name() << '\n';
  std::cout << typeid(c::any_type).name() << '\n';
}

示例 53.4 介绍了可选的模板参数。签名使用 boost::parameter::optional 作为可选的模板参数。 complicated 中的可选模板参数设置为 boost::parameter::void_,并且 boost::parameter::value_type 被赋予默认值。此默认值是可选参数将设置为的类型,如果类型未另外设置的话。

complicated 在 main() 中实例化。这次只使用参数 integral_type 和 floating_point_type。 any_type 未使用。该示例使用 Visual C++ 2013 编译,将 integral_type 的 short、floating_point_type 的 double 和 any_type 的 bool 写入标准输出。

Boost.Parameter 可以自动检测模板参数。您可以创建允许将类型自动分配给参数的签名。与函数参数一样,需要对模板元编程有更深入的了解才能做到这一点。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们