Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) C++98 std::mem_fun_ref

 

std::mem_fun_ref is an adapter to be able to use algorithms on a member function of T stored in a container as T (compare std::mem_fun, to use algorithms on a member function of T stored in a container as T* ).

 

#include <algorithm>
#include <iostream>
#include <vector>

struct MyClass
{
  void SayHello() const { std::cout << "Hello" << std::endl; }
};

 

Screen output:

 

Hello
Hello
Hello

 

 

 

 

 

 

Full example

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppMem_fun_ref.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp
QMAKE_CXXFLAGS += -Wall -Wextra -Weffc++ -Werror

 

 

 

 

 

main.cpp

 

#include <algorithm>
#include <iostream>
#include <vector>

struct MyClass
{
  void SayHello() const { std::cout << "Hello" << std::endl; }
};

int main()
{
  std::vector<MyClass> v(3);
  std::for_each(v.begin(), v.end(), std::mem_fun_ref(&MyClass::SayHello));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml