-2

According to Google C++ Style guide,

Parameters to C/C++ functions are either input to the function, output from the function, or both. Input parameters are usually values or const references, while output and input/output parameters will be pointers to non-const

In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers. Input parameters may be const pointers

reference should always be constant. What happens if I have a vector of objects that will be updated? Should I follow the guide and use pointer to vector? What happens if the object data will also be updated? Should I use pointer to vector of pointer of object, e.g. std::vector< Object*>*? I find this convention rather odd.

Am I misunderstanding it or is this the right way to do it (meaning this is really what the guide is telling people to do)?

Community
  • 1
  • 1
  • 1
    *(practically almost always)* Never use pointer to vector. Vector already holds a pointer to its data. – Galik Oct 30 '17 at 04:19
  • @Galik what about std::vector< Object*>? –  Oct 30 '17 at 04:24
  • I'd add the text from the guidelines if you've got'em to the question. Maybe they list some pros and cons. Frankly I don't see this as a good idea. – user4581301 Oct 30 '17 at 04:24
  • 2
    No, Just `std::vector` and make `Object` moveable (unless you need polymorphism) – Galik Oct 30 '17 at 04:25
  • 1
    If your intentions are to get a job with Google, then you shuld adopt their style guide. If you have no plans to get a job with Google, their style guide is just as credible as Joe Q Random's style guide. There's nothing wrong with passing around mutable references to vectors, when appropriate. – Sam Varshavchik Oct 30 '17 at 04:29
  • The Google style guide is *very* specific for solving problems Google has with their code base. For example [it says not to use exceptions](https://stackoverflow.com/questions/5184115/google-c-style-guides-no-exceptions-rule-stl) which is not a good idea at all, but forced upon them by history. When writing new code there is no reason to follow *their* rules, unless you work for Google. – Bo Persson Oct 30 '17 at 08:12

1 Answers1

2

The google style guide states that you should never have a non-const reference as a parameter to a function, because they want "parameters that may be modified" to be explicitly marked at the point of call.

Under that rule, if your function takes a vector of data it will modify, it must take a pointer to it.

On the other hand, the gsl (C++ core guidelines) provides a span<T> type that, if you are only modifying elements and not which elements the vector has, is a superior choice to taking a vector<T>* in many cases.

This disagrees in spirit, if not practice, with the google style guidelines.

Which is right is a matter of opinion, and hence off-topic at stack overflow.

Do not, however, assume that just because it is "google"s style guidelines that they are somehow perfect. Even if they are perfect for google, google is a strange company. Perfect for their development environment may not be perfect for most development in C++ on the planet.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524