Here's a simple question regarding const correctness.
I have this class:
template <class T>
class Foo
{
public:
std::map<std::string, boost::any> members;
template <typename T>
std::vector<T>& member(const std::string& memberName)
{
return boost::any_cast<std::vector<T>&>(members[memberName]);
}
};
I then have a functor which includes the following:
bool operator()(Foo& foo) const
{
std::vector<T> & member = foo.member<T>(_memberName);
What confuses me here is that I cant pass Foo by reference to const, since I'm calling the non const member getter function. With regard to its signature, this gives the impression that operator() changes foo.
Should I correct this and if so how?
Having a const getter allows you to call it on const objects: const Object obj; obj. getReady(); This is only valid if getReady is marked as const .
In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.
const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.
A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.
The usual way is to add a const
overload for the member function:
template <typename T>
std::vector<T> const & member(const std::string& memberName) const
{ ^^^^^ ^^^^^
return boost::any_cast<std::vector<T> const &>(members.at(memberName));
} ^^^^^ ^^
Calling the member on a const Foo
will choose this overload; calling it on a non-const
will choose the original one.
Note that at()
is a fairly new addition to std::map
. If you're stuck with an outdated library, you'll need something like:
std::map<std::string, boost::any>::const_iterator found = members.find(memberName);
if (found == members.end()) {
throw std::runtime_error("Couldn't find " + memberName);
}
return boost::any_cast<std::vector<T> const &>(found->second);
The const correctness applies on the object, whose method you execute. So:
bool operator()(Foo& foo) const
means that operator()
will not change anything in the functor class, like the _memberName
(which seems to be a member of the functor class).
The way it is defined, it is allowed to change Foo (call non-const methods).
EDIT: See Mike Seymour's answer as it describes a way to fix it. I personally have done that a lot but didn't seem to get exactly your question. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With