lundi 28 mai 2018

How to convert a std::map to a std::function?

A std::map<K,V> implements a partial function of type std::function<V(K)>.

I am trying to implement a generic function map2fun that turns a map into a function object.

The following does not compile:

template<typename M>
function<M::mapped_type(M::key_type)> map2fun(M& m)
{
  return [&m](M::key_type k)
    {
      return m[k];
    };
}

My questions are:

  • Is there a similar functionality available in the STL for C++11?
  • If not, how can I implement it with C++11?

Aucun commentaire:

Enregistrer un commentaire