rubenvb rubenvb -4 years ago 155
C++ Question

Implementing signals (Observer pattern): is mutable or const_cast necessary?

I'm implementing my very own

/slot (observer pattern, Qt style) mechanism so I can have a
that notifies ... stuff... that is has changed.

I think C++11 provides everything necessary to make a very succint and featureful implementation possible. The "issue" I'm running into is if I want to "connect" to a signal of a
object, I need the
function to be const, but modify the list of callbacks/observers. There are two straightforward ways to fix this:

  1. const_cast
    the lists inside

  2. Make the lists

Both seem to me like the same thing (and this has been asked before, e.g. in this question), and perfectly fine logically, but stylistically questionable. Hence the question. Is there a way around this or is this a truly justified use of

Some prelimenary code as I have it now:

template<typename... ArgTypes>
class signal
template<typename Callable>
void connect(Callable&& callback) const
std::lock_guard<std::mutex> lock(slots_mutex);

void emit(ArgTypes... arguments) const
std::lock_guard<std::mutex> lock(slots_mutex);
for(auto&& callback : slots)

// mutable here allows to connect to a const object's signals
mutable std::vector<std::function<void(ArgTypes...)>> slots;
std::mutex slots_mutex;


Note I haven't tested this code; this is just a reflection of my current state of mind.

Answer Source

mutable is usually the better choice for such cases.

Avoid (const) casting whenever you can, it's prone for hitting undefined behavior, while mutable is guaranteed to be not1).

1 mutable class members are guaranteed not to go to emitted codes .text segment for instance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download