Why mark a `std::scoped_lock` variable as const?

Viewed 190

I tried the "Code Analysis" feature of the latest Visual Studio 2017 release on a codebase which uses a number of std::scoped_lock variables like this:

void f()
{
    std::scoped_lock<std::mutex> my_lock(my_mutex);
    // ...
}

With the "Microsoft All Rules" configuration, Visual Studio suggests the following:

warning C26496: The variable 'my_lock' is assigned only once, mark it as const (con.4).

I normally use local const variables all the time, but I never felt it was necessary with types that don't allow modification anyway. A std::scoped_lock can only be constructed and destroyed. I guess Visual Studio just blindly applies a very general guideline here, but do I miss something? Would a std::scoped_lock<std::mutex> const ever allow the compiler to do something differently, or to detect an error during compilation?

0 Answers
Related