### Example: Using boost::bind2nd with boost::mem_fun_ref Source: https://www.boost.org/doc/libs/latest/libs/functional/index Demonstrates how to use Boost's binder and member function adapter to apply a member function to elements in a collection. This example highlights the advantage over standard adapters when dealing with member functions taking reference arguments. ```cpp class Person { public: void set_name(const std::string &name); // ... }; std::for_each(c.begin(), c.end(), boost::bind2nd(boost::mem_fun_ref(&Person::set_name), "Fred")); ``` -------------------------------- ### Using boost::ptr_fun with std::find_if Source: https://www.boost.org/doc/libs/latest/libs/functional/ptr_fun Demonstrates the usage of boost::ptr_fun to adapt a function pointer for use with standard algorithms like std::find_if. This example shows how to find an element in a vector that does not satisfy a given predicate. ```cpp bool bad(std::string foo) { /* ... */ } std::vector c; // ... std::vector::iterator it = std::find_if(c.begin(), c.end(), std::not1(boost::ptr_fun(bad))); ``` -------------------------------- ### Boost.Function enhanced negators with function object traits Source: https://www.boost.org/doc/libs/latest/libs/functional/ptr_fun Illustrates the use of Boost's enhanced negators, such as boost::not1, which leverage function object traits for more efficient predicate adaptation with standard algorithms. This example achieves the same result as the previous one but with potentially better performance. ```cpp std::vector::iterator it = std::find_if(c.begin(), c.end(), boost::not1(bad)); ``` -------------------------------- ### Member Function Adapters with Boost (C++) Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Demonstrates using boost::mem_fun to adapt member functions for use with standard algorithms. This example shows retrieving the 'get_name' member function's result from vectors of pointers to Person objects, including const pointers. Requires Boost.Functional. ```cpp #include #define boost std #include #include #include #include #include class Person { public: Person() {} Person(const char *n) : name(n) {} const std::string &get_name() const { return name; } void print(std::ostream &os) const { os << name << " "; } void set_name(const std::string &n) { name = n; std::cout << name << " "; } std::string clear_name() { std::string ret = name; name = ""; return ret; } void do_something(int) const {} bool is_fred() const { return name == "Fred"; } private: std::string name; }; namespace { bool is_equal(const std::string &s1, const std::string &s2) { return s1 == s2; } bool is_betty(const std::string &s) { return s == "Betty"; } void do_set_name(Person *p, const std::string &name) { p->set_name(name); } void do_set_name_ref(Person &p, const std::string &name) { p.set_name(name); } } int main() { std::vector v1; v1.push_back("Fred"); v1.push_back("Wilma"); v1.push_back("Barney"); v1.push_back("Betty"); const std::vector cv1(v1.begin(), v1.end()); std::vector v2; v2.push_back("Fred"); v2.push_back("Wilma"); v2.push_back("Barney"); v2.push_back("Betty"); Person person; Person &r = person; Person fred("Fred"); Person wilma("Wilma"); Person barney("Barney"); Person betty("Betty"); std::vector v3; v3.push_back(&fred); v3.push_back(&wilma); v3.push_back(&barney); v3.push_back(&betty); const std::vector cv3(v3.begin(), v3.end()); std::vector v3c(v3.begin(), v3.end()); std::ostream &os = std::cout; // const_mem_fun_t std::cout << '\n'; std::transform(v3.begin(), v3.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun(&Person::get_name)); std::cout << '\n'; std::transform(cv3.begin(), cv3.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun(&Person::get_name)); std::cout << '\n'; std::transform(v3c.begin(), v3c.end(), std::ostream_iterator(std::cout, " "), ``` -------------------------------- ### Using Boost.Functional not1 for std::find_if Source: https://www.boost.org/doc/libs/latest/libs/functional/negators Demonstrates how to use Boost.Functional's `not1` adapter with `std::find_if` to find elements that do not satisfy a given predicate. This example shows a common usage pattern for negating a unary function object. ```cpp bool bad(const Foo &foo) { ... } std::vector c; ... std::find_if(c.begin(), c.end(), boost::not1(bad)); ``` -------------------------------- ### Using Boost bind2nd with mem_fun_ref Source: https://www.boost.org/doc/libs/latest/libs/functional/binders Demonstrates binding the second argument of a member function reference using Boost binders. This example shows how to use boost::bind2nd with boost::mem_fun_ref to pass std::cout to a member function that takes an std::ostream reference parameter. The Boost implementation avoids compilation errors that would occur with standard library binders. ```cpp class Foo { public: void bar(std::ostream &); // ... }; // ... std::vector c; // ... std::for_each(c.begin(), c.end(), boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout)); ``` -------------------------------- ### C++ Example of const_mem_fun1_t Issue with std::bind1st Source: https://www.boost.org/doc/libs/latest/libs/functional/mem_fun This C++ code demonstrates a scenario where the standard `const_mem_fun1_t` can cause problems. It shows how `std::bind1st` might attempt to initialize a `Foo*` member with a `const Foo*` pointer, leading to a type incompatibility due to the `first_argument_type` mismatch. ```cpp struct Foo { void bar(int) const; }; const Foo *cp = new Foo; std::bind1st(std::mem_fun(&Foo::bar), cp); // This results in: typedef Foo* first_argument_type; ``` -------------------------------- ### Modifying Members with bind1st and mem_fun Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Utilizes boost::bind1st with boost::mem_fun to call a member function that requires a specific object instance and an argument. This example shows setting a name on a person object. ```C++ #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // mem_fun1_t, bind1st std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person)); #endif ``` -------------------------------- ### Pointer to Function Adapters with Boost (C++) Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Demonstrates how to use boost::ptr_fun with boost::not1 and boost::bind1st to adapt pointer-to-function objects for use with standard algorithms. This allows treating free functions as function objects. Requires Boost.Functional. ```cpp #include #define boost std #include #include #include #include #include class Person { public: Person() {} Person(const char *n) : name(n) {} const std::string &get_name() const { return name; } void print(std::ostream &os) const { os << name << " "; } void set_name(const std::string &n) { name = n; std::cout << name << " "; } std::string clear_name() { std::string ret = name; name = ""; return ret; } void do_something(int) const {} bool is_fred() const { return name == "Fred"; } private: std::string name; }; namespace { bool is_equal(const std::string &s1, const std::string &s2) { return s1 == s2; } bool is_betty(const std::string &s) { return s == "Betty"; } void do_set_name(Person *p, const std::string &name) { p->set_name(name); } void do_set_name_ref(Person &p, const std::string &name) { p.set_name(name); } } int main() { std::vector v1; v1.push_back("Fred"); v1.push_back("Wilma"); v1.push_back("Barney"); v1.push_back("Betty"); const std::vector cv1(v1.begin(), v1.end()); std::vector v2; v2.push_back("Fred"); v2.push_back("Wilma"); v2.push_back("Barney"); v2.push_back("Betty"); Person person; Person &r = person; Person fred("Fred"); Person wilma("Wilma"); Person barney("Barney"); Person betty("Betty"); std::vector v3; v3.push_back(&fred); v3.push_back(&wilma); v3.push_back(&barney); v3.push_back(&betty); const std::vector cv3(v3.begin(), v3.end()); std::vector v3c(v3.begin(), v3.end()); std::ostream &os = std::cout; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL) // pointer_to_unary_function std::cout << '\n'; std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::not1(boost::ptr_fun(is_betty))); // pointer_to_binary_function, bind1st std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person)); std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person)); std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r)); #endif return 0; } ``` -------------------------------- ### Applying Member Functions with Arguments using bind2nd and mem_fun Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Demonstrates using boost::bind2nd with boost::mem_fun to call a member function that takes an argument. This is often used with algorithms like std::for_each for actions like printing. ```C++ #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // const_mem_fun1_t, bind2nd std::cout << '\n'; std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout)); std::cout << '\n'; std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os)); #endif ``` -------------------------------- ### Binding Arguments with Boost Functional Adapters (C++) Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Demonstrates using boost::bind1st and boost::bind2nd to fix one argument of a binary function, creating a new function object. This is shown with both free functions and member functions for various use cases. Requires Boost.Functional. ```cpp #include #define boost std #include #include #include #include #include class Person { public: Person() {} Person(const char *n) : name(n) {} const std::string &get_name() const { return name; } void print(std::ostream &os) const { os << name << " "; } void set_name(const std::string &n) { name = n; std::cout << name << " "; } std::string clear_name() { std::string ret = name; name = ""; return ret; } void do_something(int) const {} bool is_fred() const { return name == "Fred"; } private: std::string name; }; namespace { bool is_equal(const std::string &s1, const std::string &s2) { return s1 == s2; } bool is_betty(const std::string &s) { return s == "Betty"; } void do_set_name(Person *p, const std::string &name) { p->set_name(name); } void do_set_name_ref(Person &p, const std::string &name) { p.set_name(name); } } int main() { std::vector v1; v1.push_back("Fred"); v1.push_back("Wilma"); v1.push_back("Barney"); v1.push_back("Betty"); const std::vector cv1(v1.begin(), v1.end()); std::vector v2; v2.push_back("Fred"); v2.push_back("Wilma"); v2.push_back("Barney"); v2.push_back("Betty"); Person person; Person &r = person; Person fred("Fred"); Person wilma("Wilma"); Person barney("Barney"); Person betty("Betty"); std::vector v3; v3.push_back(&fred); v3.push_back(&wilma); v3.push_back(&barney); v3.push_back(&betty); const std::vector cv3(v3.begin(), v3.end()); std::vector v3c(v3.begin(), v3.end()); std::ostream &os = std::cout; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL) // binary_traits, bind1st, bind2nd std::cout << '\n'; std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::bind1st(is_equal, "Betty")); std::cout << '\n'; std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::bind2nd(is_equal, "Betty")); // binary_traits std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person)); std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person)); std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r)); #endif return 0; } ``` -------------------------------- ### Binary Negation with Boost Functional Adapters (C++) Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Illustrates the use of boost::not2 in conjunction with boost::bind1st and boost::bind2nd for binary predicates like is_equal. This allows negating the comparison result when binding arguments to a binary function. Requires Boost.Functional. ```cpp #include #define boost std #include #include #include #include #include class Person { public: Person() {} Person(const char *n) : name(n) {} const std::string &get_name() const { return name; } void print(std::ostream &os) const { os << name << " "; } void set_name(const std::string &n) { name = n; std::cout << name << " "; } std::string clear_name() { std::string ret = name; name = ""; return ret; } void do_something(int) const {} bool is_fred() const { return name == "Fred"; } private: std::string name; }; namespace { bool is_equal(const std::string &s1, const std::string &s2) { return s1 == s2; } bool is_betty(const std::string &s) { return s == "Betty"; } void do_set_name(Person *p, const std::string &name) { p->set_name(name); } void do_set_name_ref(Person &p, const std::string &name) { p.set_name(name); } } int main() { std::vector v1; v1.push_back("Fred"); v1.push_back("Wilma"); v1.push_back("Barney"); v1.push_back("Betty"); const std::vector cv1(v1.begin(), v1.end()); std::vector v2; v2.push_back("Fred"); v2.push_back("Wilma"); v2.push_back("Barney"); v2.push_back("Betty"); Person person; Person &r = person; Person fred("Fred"); Person wilma("Wilma"); Person barney("Barney"); Person betty("Betty"); std::vector v3; v3.push_back(&fred); v3.push_back(&wilma); v3.push_back(&barney); v3.push_back(&betty); const std::vector cv3(v3.begin(), v3.end()); std::vector v3c(v3.begin(), v3.end()); std::ostream &os = std::cout; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL) // binary_traits, binary_negate std::cout << '\n'; std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::bind1st(boost::not2(is_equal), "Betty")); std::cout << '\n'; std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::bind2nd(boost::not2(is_equal), "Betty")); #endif return 0; } ``` -------------------------------- ### Unary Negation with Boost Functional Adapters (C++) Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Demonstrates using boost::not1 with a predicate function (is_betty) and a member function (Person::is_fred) to transform collections. It applies the negation to the result of the predicate. Requires Boost.Functional. ```cpp #include #define boost std #include #include #include #include #include class Person { public: Person() {} Person(const char *n) : name(n) {} const std::string &get_name() const { return name; } void print(std::ostream &os) const { os << name << " "; } void set_name(const std::string &n) { name = n; std::cout << name << " "; } std::string clear_name() { std::string ret = name; name = ""; return ret; } void do_something(int) const {} bool is_fred() const { return name == "Fred"; } private: std::string name; }; namespace { bool is_equal(const std::string &s1, const std::string &s2) { return s1 == s2; } bool is_betty(const std::string &s) { return s == "Betty"; } void do_set_name(Person *p, const std::string &name) { p->set_name(name); } void do_set_name_ref(Person &p, const std::string &name) { p.set_name(name); } } int main() { std::vector v1; v1.push_back("Fred"); v1.push_back("Wilma"); v1.push_back("Barney"); v1.push_back("Betty"); const std::vector cv1(v1.begin(), v1.end()); std::vector v2; v2.push_back("Fred"); v2.push_back("Wilma"); v2.push_back("Barney"); v2.push_back("Betty"); Person person; Person &r = person; Person fred("Fred"); Person wilma("Wilma"); Person barney("Barney"); Person betty("Betty"); std::vector v3; v3.push_back(&fred); v3.push_back(&wilma); v3.push_back(&barney); v3.push_back(&betty); const std::vector cv3(v3.begin(), v3.end()); std::vector v3c(v3.begin(), v3.end()); std::ostream &os = std::cout; #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL) // unary_traits, unary_negate std::transform(v2.begin(), v2.end(), std::ostream_iterator(std::cout, " "), boost::not1(is_betty)); std::cout << '\n'; std::transform(v1.begin(), v1.end(), std::ostream_iterator(std::cout, " "), boost::not1(boost::mem_fun_ref(&Person::is_fred))); #endif return 0; } ``` -------------------------------- ### Standard C++ pointer_to_unary_function declaration Source: https://www.boost.org/doc/libs/latest/libs/functional/ptr_fun Shows the standard C++ declaration for the pointer_to_unary_function template. It defines how a function pointer is wrapped and how its operator() is invoked, highlighting potential performance issues with value type arguments. ```cpp template class pointer_to_unary_function : public unary_function { public: explicit pointer_to_unary_function(Result (* f)(**Arg**)); Result operator()(**Arg** x) const; }; ``` -------------------------------- ### Boost.Function operator() using call_traits for optimized argument passing Source: https://www.boost.org/doc/libs/latest/libs/functional/ptr_fun Presents the optimized declaration of operator() within Boost.Function, utilizing the call_traits::param_type typedef. This approach dynamically determines the optimal way to pass arguments (by value or const reference) based on the argument type, improving efficiency and avoiding issues like references to references. ```cpp Result operator()(typename call_traits::param_type x) const ``` -------------------------------- ### C++ Boost.Function mem_fun1_t with call_traits Source: https://www.boost.org/doc/libs/latest/libs/functional/mem_fun This C++ code demonstrates how Boost.Function improves `mem_fun1_t` by using `call_traits::param_type` for the second argument of `operator()`. This approach optimizes argument passing, avoiding unnecessary copies for value types and preventing issues with reference types. ```cpp S operator()(T* p, typename call_traits::param_type x) const ``` -------------------------------- ### C++ Standard Library mem_fun1_t Argument Handling Source: https://www.boost.org/doc/libs/latest/libs/functional/mem_fun This C++ code snippet illustrates the definition of `mem_fun1_t` from the C++ Standard Library. It points out that the second argument to `operator()` is passed by value, which can lead to inefficiencies due to multiple copies for non-reference types. ```cpp template class mem_fun1_t : public binary_function { public: explicit mem_fun1_t(S (T::*p)(A)); S operator()(T* p, A x) const; }; ``` -------------------------------- ### Boost binder2nd constructor using call_traits Source: https://www.boost.org/doc/libs/latest/libs/functional/binders Demonstrates how Boost's enhanced binder2nd avoids the references to references problem by using call_traits templates. The constructor parameter type is determined by call_traits, which properly handles reference types and prevents illegal reference to reference formations. ```cpp binder2nd(const Operation& x, typename call_traits< typename binary_traits::second_argument_type >::param_type y) ``` -------------------------------- ### Standard binder2nd template definition with reference issue Source: https://www.boost.org/doc/libs/latest/libs/functional/binders Shows the standard C++ library binder2nd template class definition that creates the references to references problem. When the second_argument_type is a reference (e.g., std::ostream&), the constructor parameter y becomes a reference to a reference (std::ostream&&), which is illegal in C++. ```cpp template class binder2nd : public unary_function { ... public: binder2nd(const Operation& x, const typename Operation::second_argument_type& y); ... }; ``` -------------------------------- ### Calling Member Functions with mem_fun and std::transform Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Applies a member function to each element in a range using std::transform and boost::mem_fun. This is useful for operations that return a value, such as getters. ```C++ std::cout << '\n'; std::transform(v1.begin(), v1.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun(&Person::get_name)); ``` -------------------------------- ### Boost.Functional unary_negate class template definition Source: https://www.boost.org/doc/libs/latest/libs/functional/negators Illustrates the C++ Standard Library's definition of the `unary_negate` class template. This serves as a baseline for understanding the improvements provided by Boost.Functional's version, particularly regarding argument type handling. ```cpp template class unary_negate : public unary_function { public: explicit unary_negate(const Predicate& pred); bool operator()(const typename Predicate::argument_type& x) const; }; ``` -------------------------------- ### Applying Const Member Functions with Arguments using bind2nd and mem_fun_ref Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Combines boost::bind2nd and boost::mem_fun_ref to invoke const member functions that require arguments on elements within a range. Suitable for operations on const objects or references. ```C++ #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // const_mem_fun1_ref_t, bind2nd std::cout << '\n'; std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout)); std::cout << '\n'; std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os)); #endif ``` -------------------------------- ### C++ Standard Library const_mem_fun1_t Definition Source: https://www.boost.org/doc/libs/latest/libs/functional/mem_fun This C++ code snippet shows the definition of `const_mem_fun1_t` as specified in the C++ Standard Library. It highlights a potential issue with the `first_argument_type` typedef, which can lead to type mismatches when used with functions like `std::bind1st`. ```cpp template class const_mem_fun1_t : public binary_function { public: explicit const_mem_fun1_t(S (T::*p)(A) const); S operator()(const T* p, A x) const; }; ``` -------------------------------- ### Modifying Members with bind1st and mem_fun_ref Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Employs boost::bind1st with boost::mem_fun_ref to modify members of objects using a reference and an argument. This allows for in-place modification of object state. ```C++ #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // mem_fun1_ref_t, bind1st std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person)); std::cout << '\n'; std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r)); #endif ``` -------------------------------- ### Define binary_traits template with type members Source: https://www.boost.org/doc/libs/latest/libs/functional/function_traits The binary_traits template struct extracts type information from binary functions or function objects. It provides function_type, param_type, result_type, first_argument_type, and second_argument_type. This template is used for adapters that work with binary operations and allows uniform handling of plain functions and adaptable function objects. ```C++ template struct binary_traits { typedef T function_type; typedef typename T::param_type param_type; typedef typename T::result_type result_type; typedef typename T::first_argument_type first_argument_type; typedef typename T::second_argument_type second_argument_type; }; ``` -------------------------------- ### Use unary_traits in function object adapter constructor Source: https://www.boost.org/doc/libs/latest/libs/functional/function_traits Demonstrates using unary_traits::param_type in a constructor to optimally pass a function or function object as a parameter. Function objects are passed by const reference while function pointers are passed by value, providing efficient parameter passing regardless of the predicate type. ```C++ template class unary_negate { public: explicit unary_negate(typename unary_traits::param_type x) : pred(x) {} private: typename unary_traits::function_type pred; }; ``` -------------------------------- ### Boost.Functional unary_negate operator() with call_traits Source: https://www.boost.org/doc/libs/latest/libs/functional/negators Shows how Boost.Functional's `unary_negate` can declare its `operator()` argument using `call_traits<...>::param_type`. This approach efficiently handles both reference and value types for the predicate's argument, avoiding unnecessary copies and potential reference-to-reference issues. ```cpp bool operator()(typename call_traits::param_type x) const ``` -------------------------------- ### Clearing Member Data with mem_fun and std::transform Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Applies a member function, such as 'clear_name', to each element in a range using std::transform and boost::mem_fun. This is useful for resetting or clearing data within objects. ```C++ // mem_fun_t std::cout << '\n'; std::transform(v3.begin(), v3.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun(&Person::clear_name)); ``` -------------------------------- ### Calling Const Member Functions with mem_fun_ref and std::transform Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Uses std::transform with boost::mem_fun_ref to apply a const member function to elements in a range. This is suitable for accessing data from const objects or const references. ```C++ std::cout << '\n'; std::transform(cv1.begin(), cv1.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun_ref(&Person::get_name)); ``` -------------------------------- ### Define unary_traits template with type members Source: https://www.boost.org/doc/libs/latest/libs/functional/function_traits The unary_traits template struct extracts type information from unary functions or function objects. It provides function_type (the function/object type itself), param_type (optimal parameter passing type), result_type (return type), and argument_type (parameter type). Used for declaring function objects in adapters and enabling uniform treatment of functions and function objects. ```C++ template struct unary_traits { typedef T function_type; typedef typename T::param_type param_type; typedef typename T::result_type result_type; typedef typename T::argument_type argument_type; }; ``` -------------------------------- ### Clearing Member Data with mem_fun_ref and std::transform Source: https://www.boost.org/doc/libs/latest/libs/functional/test/function_test Uses std::transform with boost::mem_fun_ref to apply a member function for clearing data to elements in a range. This is efficient for modifying object state directly via references. ```C++ // mem_fun_ref_t std::cout << '\n'; std::transform(v1.begin(), v1.end(), std::ostream_iterator(std::cout, " "), boost::mem_fun_ref(&Person::clear_name)); ``` -------------------------------- ### Replace standard traits with boost unary_traits Source: https://www.boost.org/doc/libs/latest/libs/functional/function_traits Shows how to use boost::unary_traits to extract argument types from a predicate template parameter instead of directly accessing Operation::argument_type. This approach works uniformly with both plain functions and adaptable function objects, eliminating the need for ptr_fun in most cases. ```C++ typename boost::unary_traits::argument_type ``` -------------------------------- ### Use unary_traits in function object adapter declaration Source: https://www.boost.org/doc/libs/latest/libs/functional/function_traits Demonstrates using unary_traits::function_type to declare a predicate member in a template class that can accept both function pointers and adaptable function objects. The function_type typedef allows proper instantiation of template classes with function types, which would otherwise be impossible due to C++ template specialization rules. ```C++ template class unary_negate { private: typename unary_traits::function_type pred; }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.