Sunday, August 13, 2017

Binding std::function to member functions

I realized that std::function can be bound to member functions without requiring the *this object. Consider the following examples.
// std::string::empty is a const function. All variables from e1 to e5 are fine.
std::function<bool(std::string)> e1 = &std::string::empty;
std::function<bool(std::string &)> e2 = &std::string::empty;
std::function<bool(const std::string &)> e3 = &std::string::empty;
std::function<bool(std::string *)> e4 = &std::string::empty;
std::function<bool(const std::string *)> e5 = &std::string::empty;

// std::string::push_back is not a const function. p4 and p5 don't compile.
std::function<void(std::string, char)> p1 = &std::string::push_back;
std::function<void(std::string &, char)> p2 = &std::string::push_back;
std::function<void(std::string *, char)> p3 = &std::string::push_back;

// These two don't compile because push_back is a non-const function 
std::function<void(const std::string &, char)> p4 = &std::string::push_back;
std::function<void(const std::string *, char)> p5 = &std::string::push_back;
I thought I knew how to do that but this time I found that the syntax is a little different than what I had in mind.

I used tho think (incorrectly) that just like function types for free-standing functions, one would create a member-function type. While, it's straight-forward to create function types for free-standing functions, I think it's not possible to create member function types. Don't get me wrong. One can create pointer-to-member-function type just like pointer-to-a-function type. Here's what I mean.
using F = int(const char *); // a function type of a free standing function.
F f = std::atoi; // error because a there are no instances of a function type.
F* fnew = new F(); //error because new can't be applied to a function type.
F* fptr = &std::atoi; // OK. A pointer to a function type is initialized.

// However, there's no function type for member functions
using GPTR = bool (std::string::*)() const; // OK. This is a pointer-to-member-function-type. 
GPTR gptr = &std::string::empty; // OK. This is a pointer to a member function.
string s;
std::cout << (s.*gptr)(); // OK. prints 1
using H = decltype(*gptr); // error. It makes no sense without a std:string object. Illformed.

bool (*x)(const std::string &) = gptr; // error. Incompatible types.
std::function<bool (const std::string &)> fobj = gptr; // OK! Neat!
Therefore, std::function uses the syntax of free-standing function types to bind to pointer to member functions. That's neat.

Sunday, August 06, 2017

Avoiding intermediate copies in std::accumulate

std::accumulate makes a ton of copies internally. In fact it's 2x the size of the number of elements in the iterator range. To fix, use std::ref and std::reference_wrapper for the initial state. std::shared_ptr is also a possibility if the accumulated state must be dynamically allocated for some reason. Live code on wandbox.

Update: Please see alternative solutions in the comments section.

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <functional>

struct Vector : public std::vector<int> {
  Vector(std::initializer_list<int> il) : std::vector<int>(il){
    std::cout << "Vector(std::initializer_list)\n";
  }
  Vector() {
    std::cout << "Vector()\n";
  }
  Vector(const Vector &v) : std::vector<int>(v) {
    std::cout << "Vector(const Vector &)\n";
  }
  Vector & operator = (const Vector &v) {
    if (this != &v) {
      std::vector<int>::operator=(v);
      std::cout << "Vector& Vector::operator=(const Vector &)\n";
    }
    return *this;
  }
  Vector & operator = (Vector && v) {
    if (this != &v) {
      std::vector<int>::operator=(std::move(v));
      std::cout << "Vector& Vector::operator=(Vector&&)\n";
    }
    return *this;
  }
  Vector(Vector&& v) : std::vector<int>(std::move(v)) {
     std::cout << "Vector(Vector&&)\n";
  }
};

void copy_accumulate(Vector &v) {
    Vector v2 = std::accumulate(v.begin(), v.end(), Vector(), 
                    [](Vector & v, int i) {
                      v.push_back(i);
                      return v;
                    });
    std::cout << "size of v2 = " << v2.size() << "\n";
}

void nocopy_accumulate(Vector &v) {
    Vector init;
    Vector v2 = std::accumulate(v.begin(), v.end(), std::ref(init), 
                    [](std::reference_wrapper<Vector> v, int i) {
                      v.get().push_back(i);
                      return v;
                    });
    std::cout << "size of v2 = " << v2.size() << "\n";
}

int main()
{
    Vector v { 1, 2, 3, 4 };
    copy_accumulate(v);
    std::cout << "=============\n";
    nocopy_accumulate(v);
}