[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/utility/functional/mem_fun [Back]  [Original]

std::mem_fun cppreference.com
cppreference.com

std::mem_fun

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
(C++20)(C++23)
(C++11)
(C++17)(C++23)
(C++20)
(C++11)(C++11)
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

( C++17*)
( C++17*)
( C++17*)( C++17*)( C++17*)( C++17*)
( C++20*)
( C++20*)
( C++17*)( C++17*)
( C++17*)( C++17*)

( C++17*)
( C++20*)
( C++20*)
 
<tbody> </tbody>
template< class Res, class T > std::mem_fun_t<Res,T> mem_fun( Res (T::*f)() );
(1) ( C++11)
( C++17)
template< class Res, class T > std::const_mem_fun_t<Res,T> mem_fun( Res (T::*f)() const );
(1) ( C++11)
( C++17)
template< class Res, class T, class Arg > std::mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) );
(2) ( C++11)
( C++17)
template< class Res, class T, class Arg > std::const_mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) const );
(2) ( C++11)
( C++17)

- -, . - T operator().

1) std::mem_fun_t<Res,T>(f) std::const_mem_fun_t<Res,T>(f).
2) std::mem_fun1_t<Res,T,Arg>(f) std::const_mem_fun1_t<Res,T,Arg>(f).

C++11 C++17 std::mem_fn std::bind, -.

f -

, f.

, .

std::mem_fun std::mem_fun_ref , , , — .

std::mem_fun std::mem_fn. , C++11/14: g++/clang++ -std=c++11, cl /std:c++11, .. , , gcc-12, "deprecated declaration", C++98.

#include <functional>
#include <iostream>

struct S
{
    int get_data() const { return data; }
    void no_args() const { std::cout << "void S::no_args() const\n"; }
    void one_arg(int) { std::cout << "void S::one_arg()\n"; }
    void two_args(int, int) { std::cout << "void S::two_args(int, int)\n"; }
#if __cplusplus > 201100
    int data{42};
#else
    int data;
    S() : data(42) {}
#endif
};

int main()
{
    S s;
 
    std::const_mem_fun_t<int, S> p = std::mem_fun(&S::get_data);
    std::cout << "s.get_data(): " << p(&s) << '\n';
 
    std::const_mem_fun_t<void, S> p0 = std::mem_fun(&S::no_args);
    p0(&s);

    std::mem_fun1_t<void, S, int> p1 = std::mem_fun(&S::one_arg);
    p1(&s, 1);

#if __cplusplus > 201100
//  auto p2 = std::mem_fun(&S::two_args); // : mem_fun  
                                          // -   
                                          //    .  ,
                                          // std::mem_fn  :
    auto p2 = std::mem_fn(&S::two_args);
    p2(s, 1, 2);

//  auto pd = std::mem_fun(&S::data); // :    
                                      //  .
                                      //    std::mem_fn:
    auto pd = std::mem_fn(&S::data);
    std::cout << "s.data = " << pd(s) << '\n';
#endif
}

:

s.get_data(): 42
void S::no_args() const
void S::one_arg(int)
void S::two_args(int, int)
s.data = 42

(C++11)

( ) []
( C++11)( C++17)
-,
( ) []

Web Proxy Viewer  |  New URL  |  Original Page