jeudi 1 juillet 2021

C++11. auto in lamda

I have the next code (C++11):

...

ATTRIBUTE_PROBLEM getAttrWithCustomType(const std::string& attrName) {
   auto attr = std::find_if(
                attributes.cbegin(),
                attributes.cend(),
                [&attrName](const auto& pair) { return pair.first == attrName; }
  ...

}

std::vector<std::pair<std::string, std::string>> attributes;

Configuration in my CMake file:

set_target_properties(HlsParser PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
    )

gcc version: gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516

If this code written in the .hpp file I got the next error. And it's expected behavior, C++11 doesn't support generic lambda.

error: use of ‘auto’ in lambda parameter declaration only available with -std=c++14 or -std=gnu++14
                 [&attrName](const auto& pair) { return pair.first == attrName; }

My question:

BUT If I put this code to the .cpp file I haven't error while compilation. Could someone explain me this behavior?

Aucun commentaire:

Enregistrer un commentaire