index.rst
来自「Boost provides free peer-reviewed portab」· RST 代码 · 共 221 行
RST
221 行
+++++++++++++++++++++++++++++++++++++++++++++++++ The Boost Parameter Library +++++++++++++++++++++++++++++++++++++++++++++++++|(logo)|__.. |(logo)| image:: ../../../../boost.png :alt: Boost__ ../../../../index.htm-------------------------------------:Abstract: Use this library to write functions and class templates that can accept arguments by name: .. parsed-literal:: new_window("alert", **_width=10**, **_titlebar=false**); smart_ptr< Foo , **deleter<Deallocate<Foo> >** , **copy_policy<DeepCopy>** > p(new Foo); Since named arguments can be passed in any order, they are especially useful when a function or template has more than one parameter with a useful default value. The library also supports *deduced* parameters; that is to say, parameters whose identity can be deduced from their types... @jam_prefix.append(''' project test : requirements <include>. <source>/boost//headers ;''').. @example.prepend(''' #include <boost/parameter.hpp> namespace test { BOOST_PARAMETER_NAME(title) BOOST_PARAMETER_NAME(width) BOOST_PARAMETER_NAME(titlebar) BOOST_PARAMETER_FUNCTION( (int), new_window, tag, (required (title,*)(width,*)(titlebar,*))) { return 0; } BOOST_PARAMETER_TEMPLATE_KEYWORD(deleter) BOOST_PARAMETER_TEMPLATE_KEYWORD(copy_policy) template <class T> struct Deallocate {}; struct DeepCopy {}; namespace parameter = boost::parameter; struct Foo {}; template <class T, class A0, class A1> struct smart_ptr { smart_ptr(Foo*); }; } using namespace test; int x = ''');.. @test('compile')-------------------------------------:Authors: David Abrahams, Daniel Wallin:Contact: dave@boost-consulting.com, dalwan01@student.umu.se:Organization: `Boost Consulting`_:Date: $Date: 2005/07/18 20:34:31 $:Copyright: Copyright David Abrahams, Daniel Wallin 2005. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).. _`Boost Consulting`: http://www.boost-consulting.com.. _concepts: http://www.boost.org/more/generic_programming.html#concept-------------------------------------[Note: this tutorial does not cover all details of the library. Please see also the `reference documentation`__\ ]__ reference.html.. contents:: **Table of Contents** :depth: 2.. role:: concept :class: concept.. role:: vellipsis :class: vellipsis.. section-numbering::-------------------------------------============ Motivation============In C++, arguments_ are normally given meaning by their positionswith respect to a parameter_ list: the first argument passed mapsonto the first parameter in a function's definition, and so on.That protocol is fine when there is at most one parameter with adefault value, but when there are even a few useful defaults, thepositional interface becomes burdensome:* .. compound:: Since an argument's meaning is given by its position, we have to choose an (often arbitrary) order for parameters with default values, making some combinations of defaults unusable: .. parsed-literal:: window* new_window( char const* name, **int border_width = default_border_width,** bool movable = true, bool initially_visible = true ); const bool movability = false; window* w = new_window("alert box", movability); In the example above we wanted to make an unmoveable window with a default ``border_width``, but instead we got a moveable window with a ``border_width`` of zero. To get the desired effect, we'd need to write: .. parsed-literal:: window* w = new_window( "alert box", **default_border_width**, movability);* .. compound:: It can become difficult for readers to understand the meaning of arguments at the call site:: window* w = new_window("alert", 1, true, false); Is this window moveable and initially invisible, or unmoveable and initially visible? The reader needs to remember the order of arguments to be sure. * The author of the call may not remember the order of the arguments either, leading to hard-to-find bugs... @ignore(3)-------------------------Named Function Parameters-------------------------.. compound:: This library addresses the problems outlined above by associating each parameter name with a keyword object. Now users can identify arguments by name, rather than by position: .. parsed-literal:: window* w = new_window("alert box", **movable_=**\ false); // OK!.. @ignore()---------------------------Deduced Function Parameters---------------------------.. compound:: A **deduced parameter** can be passed in any position *without* supplying an explicit parameter name. It's not uncommon for a function to have parameters that can be uniquely identified based on the types of arguments passed. The ``name`` parameter to ``new_window`` is one such example. None of the other arguments, if valid, can reasonably be converted to a ``char const*``. With a deduced parameter interface, we could pass the window name in *any* argument position without causing ambiguity: .. parsed-literal:: window* w = new_window(movable_=false, **"alert box"**); // OK! window* w = new_window(**"alert box"**, movable_=false); // OK! Appropriately used, a deduced parameter interface can free the user of the burden of even remembering the formal parameter names... @ignore()--------------------------------Class Template Parameter Support--------------------------------.. compound:: The reasoning we've given for named and deduced parameter interfaces applies equally well to class templates as it does to functions. Using the Parameter library, we can create interfaces that allow template arguments (in this case ``shared`` and ``Client``) to be explicitly named, like this: .. parsed-literal:: smart_ptr<**ownership<shared>**, **value_type<Client>** > p; The syntax for passing named template arguments is not quite as natural as it is for function arguments (ideally, we'd be able to write ``smart_ptr<ownership=shared,鈥
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?