3 ms·
Use of templates isn't frowned upon. TMP is frowned upon because usually the performance gains don't justify the loss of readability. It has been said many time
by sgeisenh 11y ago
Use of templates isn't frowned upon. TMP is frowned upon because usually the performance gains don't justify the loss of readability. It has been said many times in many ways: don't write code for yourself, write it for others.
Basically, Google has an enormous code base. Stylistic decisions should have semantic weight, but since everybody has their own style, it becomes challenging to understand the reasoning behind certain choices. When you have a standard, it means that you can read into some of the stylistic decisions sprinkled throughout the code.
Take for example, the use of pointer arguments for passing the addresses of results. The justification is that using a pointer is explicit and unambiguous. Non-const references have value syntax but pointer semantics, which can be confusing. Yes, this is contrary to some of the core values behind C++ but Google felt that the improved readability was worth that dissonance.
- 72deluxe 11y agoI would have thought that non-const references was better? It is clear that a non-const reference can be modified. If I just wanted to let them read a value, a const reference would be clear. Do Google enforce const pointers for arguments that define where to store results? If not, you could delete the pointer they pass you. A pointer does not always indicate who owns the data clearly. Far better would be to return the values from the function (or std::move them out if you must) instead of using a parameter as a "dump your results here" flag. Stroustrup encourages this (see 5.3.3 of C++ 4th edition where he says "I don’t consider returning results through arguments particularly elegant"). IMHO, it would be better to return a std::pair<bool, std::vector<myWonderfulType>> where bool indicates the success/failure of the function, with pair.second item being the results.
- zarkov99 11y agoPointers have the advantage of signaling a return value at the call site without having to look up the function prototype.
- pjmlp 11y agoWhich I don't agree with, given the amount of pointers that fly around in C codebases. You cannot ever known what a pointer is used for. If the prototype has const, maybe just maybe, assuming the function doesn't cast it away, it is a read only. So I never bought into this argument.