Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const Correctness for Getter Function

Tags:

c++

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?

like image 609
Baz Avatar asked Sep 14 '12 14:09

Baz


People also ask

Can getter be const?

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 .

What is const correctness in C?

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.

Does using const improve performance?

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.

When should a method be const?

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.


2 Answers

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);
like image 94
Mike Seymour Avatar answered Sep 27 '22 21:09

Mike Seymour


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. :)

like image 20
Lyubomir Vasilev Avatar answered Sep 27 '22 20:09

Lyubomir Vasilev