-1

Given is a template function which checks if some elements stored in a container (specified by numElems) are equal to any of the passed elements elems.

template<typename MyType>
bool Container<MyType>::elemsEqual(const int & numElems, const std::initializer_list<MyType>& elems)
{
     for (int i = 0; i < numElems; i++) {
         const MyType& currElem = getElem(i);
             if (std::none_of(elems.begin(), elems.end(), [](MyType& elem) {return currElem == elem; })) {
                 return false;
             }       
     }
     return true;
}

Compilation aborts with the error message:

'currElem' cannot be implicitly captured because no default capture mode has been specified

What is wrong here and how can I fix this problem?

user1056903
  • 921
  • 9
  • 26

1 Answers1

4

You need to specify how you want to capture the local variables, either by value (creating a copy) :

[=](MyType& elem)

Or by reference:

[&](MyType& elem)
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 1
    Thx! ``MyType`` must be ``const`` in my case, so ``[=](const MyType& elem)`` respectively ``[&](const MyType& elem)`` is completely correct. – user1056903 Oct 08 '16 at 21:43