jeudi 25 décembre 2014

Automatic generation of const overload for member function

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://ift.tt/1EnAtY4


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://ift.tt/1EnAtY6


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://ift.tt/1CUl3pP)


Aucun commentaire:

Enregistrer un commentaire