M
M
Mercury132016-01-07 18:59:18
C++ / C#
Mercury13, 2016-01-07 18:59:18

C++ lambdas: how to make a function that takes a lambda expression?

I'm working with QAbstractItemModel and I have to do something like this.
• Test 1 failed → return QVariant().
• Test 2 failed → return QVariant().
All checks passed - we display some information.
Decided to rewrite this business on lambdas.
Let's simplify the task.

struct Entry {
  float lo, hi;
};

class GetData {
public:
  virtual QVariant get(const Entry& en) const = 0;
};

QVariant GetLo : public GetData {
public:
  QVariant get(const Entry& en) const override { return en.lo; }
  static const GetLo INSTANCE;
}

QVariant entryToVar(const Entry* en, const GetData& getData) {
  if (en == nullptr) return QVariant();
  return getData.get(*en);
}

…
QVariant vLo = entryToVar(pEntry, GetLo::INSTANCE);

How to rewrite all this in lambdas? What parameter to put instead of const GetData& getData?
If you make a template, under what conditions will the template unravel into the same thing?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question