Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic generation of const overload for member function

Tags:

c++

c++11

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:

  1. "The result of this function will have the same const-ness as one of its arguments."
  2. "The result of this member function will have the same constness as the instance you call it on."

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:

  1. Have I missed something? Is it possible to implement the second case without resorting to macros or code duplication?
  2. Anyone know if there's a WG21 standards proposal on this subject? (not necessarily inout, just anything on topic)
  3. What's the industry standard for dealing with this problem? (Here's a perhaps not-so-terrible option I was toying with: http://ideone.com/PW0vK4)
like image 244
Mark Avatar asked Dec 26 '14 07:12

Mark


People also ask

What is const operator C++?

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.

How is it possible to have both const and non-const version of a function?

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.

Why we use const in operator overloading in C++?

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.

Can const function be overloaded?

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.


1 Answers

Short answers

  1. No and no.
  2. Barely related: more options for method overloading will come with Concepts Light.
  3. Implement two method overloads, one 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: )

like image 124
Dimitar Asenov Avatar answered Oct 04 '22 16:10

Dimitar Asenov