Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) operator()

 

operator()() (pronounciation: 'function call operator') is the operator typically used by functors.

 

 

 

 

 

Example

 

#include <iostream>

struct Test
{
  //Definition of operator()
  void operator()() const
  {
    std::cout << ".\n";
  }
};

int main()
{
  //Create a Test
  Test t;

  //Call operator()
  t();
}

 

 

 

 

 

Example: GreaterThan

 

#include <algorithm>
#include <cassert>
#include <functional>
#include <numeric>
#include <vector>

//From http://www.richelbilderbeek.nl/CppFunctorGreaterThan.htm
struct GreaterThan : public std::binary_function<int,int,int>
{
  explicit GreaterThan(const int any_x = 0) : x(any_x) {}
  int operator()(const int sum, const int y) const
  {
    return sum + (y <= x ? 0 : y);
  }

  private:
  int x;
};

const std::vector<int> CreateVector()
{
  std::vector<int> v;
  v.push_back(-1);
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  return v;
}

int main()
{
  const std::vector<int> v = CreateVector();
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(5))==0);
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(4))==0);
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(3))==4);
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(2))==7);
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(1))==9);
  assert(std::accumulate(v.begin(),v.end(),0,GreaterThan(0))==10);
}

 

 

 

 

 

Advice

 

 

 

 

 

 

Reference

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 19.5. Advice. page 576: '[2] Use operator()() for call semantics, for subscripting, and for selection based on a multiple values'

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict