¿Pueden las expresiones lambda capturar variables calificadas por CV?

En el siguiente fragmento, _filewatcher is a const-qualified lvalue reference. I would like to compare it with every member of m_files, usando su getPath() member function. Unfortunately this member function can only be called on const objects. This led me to write this code:

ErrorCode DirectoryWatcherWindows::addMonitoredFile(const FileWatcherWindows& _fileWatcher)
    ErrorCode ret = Success;

    auto iter = std::find_if(m_files.begin(), m_files.end(),            
        [&](FileWatcherWindows* fileWatcher)
            return _fileWatcher.getPath() == fileWatcher->getPath();

    if (iter == m_files.end())
        ret = AlreadyExists;

    return ret;

This does not compile under MSVC10, as _fileWatcher inside the lambda expression is not const-qualified. I have tried changing the capture field to [const&] but this would not work either.

Is there any way to have CV-qualified references in captures?

preguntado el 10 de marzo de 12 a las 16:03

I don't think there's any problem: Capturing by reference captures whatever you want, but of course the reference will have the same CV-qualification as the original object. In your example, _fileWatcher is a const reference, so it's captured as const, too, and you mustn't change it. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.