Saturday, August 5, 2017

C++0x, using decltype to implement property getters/setters

I realize this is old-hat/obvious to many people, but I found it interesting...

With the addition of decltype to C++, it's now possible to write totally generic getter/setter methods to expose member variables, which do not need to be updated if the type of the member is later updated. To wit:

Obviously, just remove the non-const members to create a read-only accessor, etc. It doesn't really save on typing or anything, but it might be nice as a "standard format" accessor template and such.

No comments: