SWI-Prolog / packages-cpp

The SWI-Prolog C++ interface
30 stars 14 forks source link

Mark appropriate methods "const" #1

Closed vmatare closed 8 years ago

vmatare commented 8 years ago

Many methods like PlTerm::name() that don't actually modify *this weren't marked const, which made handling const PlTerm (and others) rather unwieldy.

This patch addresses [https://github.com/SWI-Prolog/issues/issues/39]. It should allow proper usage of const PlTerms without breaking existing code. The supplied test code compiles and runs with it. Still, I'd recommend getting at least one other person's (preferrably a cpp interface user's) opinion on this. Technically, even the unification operator =(...) could be marked const since it doesn't modify any class attributes, but I'd find that misleading in that it makes the abstraction more leaky. Not sure what the const policy should be on methods that modify the contents of pointer-type attributes.

JanWielemaker commented 8 years ago

I'm not much into const issues and certainly not a C++ expert. I can only comment on what happens. The results of these methods depend on *this pointing at a term handle. The term handle is not changed. What it refers to can be modified, causing these functions to return different values, despite the fact that *this has not changed. So, roughly I'd say this is the same as *this containing a pointer, where the pointer is never changed, but what is reachable behind it does. Methods like name() do not change anything. The various unification primitives do.

Sounds like using const is fine? If nobody comes with a counter argument I'll merge this.

vmatare commented 8 years ago

To clarify, I was merely pointing out that even the unification operators could technically be marked const, though this pull request does not do that, for the reason stated above.

JanWielemaker commented 8 years ago

Ok. I can't see much wrong with it. I merged it in the devel version. Thanks!