0

I have a function which takes another function as an argument:

T reduce(T* pBegin, T* pEnd, T(*operate)(T, T))
{
    T temp = *pBegin;
    for (T* p = pBegin + 1; p < pEnd; p++)
        temp = operate(temp, *p);
    return temp;
}

This is the way it is used:

int vec[] = { 12,14,6,-8 };
cout << reduce<int>(vec, vec + 4,
    [](int x, int y){ return (x > y) ? x : y; }
);

Why is the function name []? Is it because of the vec array? Could it be something else?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • 1
    You are using a lambda, they don't have any name: https://en.cppreference.com/w/cpp/language/lambda – UnholySheep Aug 10 '20 at 21:28
  • 1
    You don't have to pass a lambda - you could pass any function. A lambda doesn't have a name - it has a list of capture arguments inside the `[]` followed by the parameters. – Jerry Jeremiah Aug 10 '20 at 21:34
  • I have to admit that is a hard thing to Google without already knowing it's a lambda expression. – user4581301 Aug 10 '20 at 21:44
  • Re: “I have a function” — no, you have a function **template** (although it’s ill-formed). – Pete Becker Aug 10 '20 at 22:16

0 Answers0