In a subclass, I have a private std::mutex m
field that I use in an implementation of a base class pure virtual method to return a value in a thread-safe way (the value can be updated by another thread):
int SubClass::get() const // implements ‘virtual int get() = 0 const‘ of the base class
{
std::lock_guard<std::mutex> lck(m);
return value;
}
The compiler is telling me that this violates const correctness by producing an error:
error: binding ‘const std::mutex‘ to reference of type ‘std::lock_guard::mutex_type& {aka std::mutex&}‘ discards qualifiers
Is there a way to make this compliant and use std::lock_guard
in a const-correct way? Simply changing it to const std::lock_guard
changes nothing. I don‘t really understand which part is problematic, then again I‘m quite new to C++ ...
m
a member ofSubClass
? – 1201ProgramAlarm Jan 7 ‘18 at 0:13