4 ms·
Am I the only one that just cringed reading the keyword replacement section? To me the example given seemed absolutely crazy. Can anyone validate this usage of
by CorvusCrypto 10y ago
Am I the only one that just cringed reading the keyword replacement section? To me the example given seemed absolutely crazy. Can anyone validate this usage of the "feature"? It just seems so off.
- en4bz 10y agoIt's kinda useful for testing private functions and accessing private variables during testing. However using something like the `GOOGLE_FRIEND_TEST` marcro in Google Test is probably a better way to get around private functions/members.
- anuragsoni 10y agoWouldn't testing the public interface that calls the private code, be enough in most situations? If they are few and simple I test them indirectly by testing the public interface that makes use of them, otherwise they can factored out to a new class/module where they can be tested directly.
- anuragsoni 10y agoI can't think of situations where i'd ever resort to doing this (But then, maybe I haven't been using C++ long enough to come across this in a codebase that I work with). I just think that doing this can break things and make it _really_ difficult to debug.
- user5994461 10y agoOf course, the example given is insane, like most example of obscure features that should never be used in real code. It works though, "works" as in "it will compile".
- humanrebar 10y ago> Can anyone validate this usage of the "feature"? It's considered bad practice, yep. I would absolutely reject any code review that abused the preprocessor like that. The only legitimate way to use it I've seen is for "white box" testing... when you have a nontrivial amount of private helper code and you want to be able to test it. Then defining "private" to be "public" for only that test compilation cuts down on the amount of work needed with basically no risk. That being said, I prefer refactoring out all that private data and code into utilities and such. There are other ways to get good encapsulation in C++, if that's the concern.