I am attempting to implement the semantics of the D language keyword inout
in C++ (just for the fun of it).
Depending on context, it essentially says one of two things:
Useful when writing member functions like begin
and end
, among other cases.
I was able to create a valid implementation of the first case with relative ease (though the interface sure isn't pretty): http://ideone.com/wgaDJJ
The second, however, seems to be impossible by my best guess. You can only get this sort of "automatic const overload generation" when you use templates, but you can't template over the "this" argument, to my knowledge. In particular, you'll note that this code does not work/compile: http://ideone.com/W4fTa4
Was wondering:
inout
, just anything on topic)In C++ programming, when we implement the methods for classes, we usually add const specifier to make sure that the members of the class instance will not be modified by the method.
There are legitimate uses of having two member functions with the same name with one const and the other not, such as the begin and end iterator functions, which return non-const iterators on non-const objects, and const iterators on const objects, but if it's casting from const to do something, it smells like fish.
The const qualifier ensures that the parameter (param) is not altered inside the operator=() method. The above method alters the right hand side operand 'param' after assigning the value to the left hand side operand.
Overloading on the basis of const type can be useful when a function returns a reference or pointer. We can make one function const, that returns a const reference or const pointer, and another non-const function, that returns a non-const reference or pointer.
Short answers
const
and one non-const
(essentially, your last example).More details
What you want to achieve in point 2
The result of this member function will have the same constness as the instance you call it on.
is a variation of the more general:
Do something different in this method depending on the constness of the instance you call it on.
The only way to do this in C++ is to have two method overloads that only differ in their constness. Since the type of this
needs to be statically known, you cannot write a single method that works with two different types for this
. Even with template methods, you have to explicitly write (or omit) the const
modifier as there is no option to automatically generate a different template instantiation based just on the constness of this
. You need to "externalize" this
in some way if you want to be able to write a uniform template method. Essentially what you have done in your last example.
As for the industry standard, you need only look at the standard library itself. All the containers for example have method overloads for the begin()
/end()
methods that only differ by the constness, specifically to implement the functionality you want. (e.g.: http://en.cppreference.com/w/cpp/container/vector/begin)
To my knowledge there is no particular proposal to implement a similar feature in the next standard. When it comes to overloading, one big new addition that might make it to C++17 is Concepts Light, which does add new ways to overload methods based on concepts. See some of the presentations/videos by Andrew Sutton on the topic (e.g.: https://www.youtube.com/watch?v=NZeTAnW5LL0, near the 50 minute mark, although you might want to watch the entire thing for background: )
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