As with many other adaptors, it is usually inconvenient to use mem_fun1_ref_t 's constructor directly. It is usually better to use the helper function mem_fun_ref [2] instead.
Example
Given a vector of vectors, extract one element from each vector.
int main() {
int A1[5] = {1, 2, 3, 4, 5};
int A2[5] = {1, 1, 2, 3, 5};
int A3[5] = {1, 4, 1, 5, 9};
vector > V;
V.push_back(vector(A1, A1 + 5));
V.push_back(vector(A2, A2 + 5));
V.push_back(vector(A3, A3 + 5));
int indices[3] = {0, 2, 4};
int& (vector::*extract)(vector::size_type);
extract = vector::operator[];
transform (V.begin(), V.end(), indices, ostream_iterator(cout, "\n"), mem_fun_ref(extract));
}
Definition
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Template parameters
Parameter |
Description |
Result |
The member function's return type. |
X |
The class whose member function the mem_fun1_ref_t invokes. |
Arg |
The member function's argument type. |
Model of
Adaptable Binary Function
Type requirements
X has at least one member function that takes a single argument of type Arg and that returns a value of type Result . [1]
Public base classes
binary_function
Members
Member |
Where defined |
Description |
first_argument_type |
Adaptable Binary Function |
The type of the first argument: X |
second_argument_type |
Adaptable Binary Function |
The type of the second argument: Arg |
result_type |
Adaptable Binary Function |
The type of the result: Result |
Result operator()(X& x, Arg a) const |
Binary Function |
Function call operator. Invokes x.f(a) , where f is the member function that was passed to the constructor. |
explicit mem_fun1_ref_t(Result (X::*f)(Arg)) |
mem_fun1_ref_t |
See below. |
template mem_fun1_ref_t mem_fun_ref(Result (X::*f)(Arg)); [2] |
mem_fun1_ref_t |
See below. |
New members
These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_ref_t .
Member |
Description |
explicit mem_fun1_ref_t(Result (X::*f)(Arg)) |
The constructor. Creates a mem_fun1_ref_t that calls the member function f . |
template mem_fun1_ref_t mem_fun1_ref(Result (X::*f)(Arg)); [2] |
If f is of type Result (X::*)(Arg) then mem_fun_ref(f) is the same as mem_fun1_ref_t(f) , but is more convenient. This is a global function, not a member function. |
Notes
[1] The type Result is permitted to be void . That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return . At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun1_ref_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun1_ref_t with member functions whose return type is void .
[2] This helper function was called mem_fun1_ref in drafts of the C++ standard, but it is called mem_fun_ref in the final standard. This implementation provides both versions for backward compatibility, but mem_fun1_ref will be removed in a future release.
See also
mem_fun_t , mem_fun_ref_t , mem_fun1_t
Category: utilities
Component type: concept
Description
A type is Assignable if it is possible to copy objects of that type and to assign values to variables.
Notation
X
A type that is a model of Assignable
x, y
Object of type X
Valid expressions
Name |
Expression |
Return type |
Copy constructor |
X(x) |
X |
Copy constructor |
X x(y); X x = y; |
|
Assignment |
x = y [1] |
X& |
Swap |
swap(x,y) |
void |
Expression semantics
Name |
Expression |
Semantics |
Postcondition |
Copy constructor |
X(x) |
|
X(x) is a copy of x [2] |
Copy constructor |
X(x) |
|
X(x) is a copy of x [2] |
Copy constructor |
X x(y); X x = y; |
|
x is a copy of y [2] |
Assignment |
x = y [1] |
|
x is a copy of y [2] |
Swap |
swap (x,y) |
Equivalent to { X tmp = x; x = y; y = tmp; } |
|
Models
• int
Notes
[1] One implication of this requirement is that a const type is not Assignable. For example, const int is not Assignable: if x is declared to be of type const int , then x = 7 is illegal. Similarly, the type pair is not Assignable.
[2] The reason this says " x is a copy of y ", rather than " x == y ", is that operator== is not necessarily defined: equality is not a requirement of Assignable. If the type X is EqualityComparable as well as Assignable, then a copy of x should compare equal to x .
See also
DefaultConstructible
Category: utilities
Component type: concept
Description
A type is DefaultConstructible if it has a default constructor, that is, if it is possible to construct an object of that type without initializing the object to any particular value.
Notation
X
A type that is a model of DefaultConstructible
Читать дальше