Consider this code:
struct B {}; struct D1 : B {}; struct D2 : B {}; #define get if (s) return d1; else return d2; volatile bool s; struct C { const B& f() const { get } B& f() { get } private: D1 d1; D2 d2; }; Here it can be seen that different versions of f() should return the same object (in one case - constant, in the other - not), based on some selection logic, which can be quite complex. In the example, macro substitution via #define used to eliminate duplication of the code of this logic.
Is it possible to avoid duplication of code in different versions of f() without resorting to the services of a preprocessor?