0

I'm converting my code to follow the google C++ style guide. The Reference Arguments rule says "All parameters passed by reference must be labeled const" and "input arguments are values or const references while output arguments are pointers".

With respect to the signature void MyTable::LoadTable(ifstream &fin), how can I label the parameter fin const given LoadTable invokes some non-const function of fin, e.g. fin.seekg? I think fin should be regarded as an input/output parameter thus it's a little bit special. What will googlers do in this situation?


UPDATE: I knew there are lots of criticisms about the google style guide. I just wondered how googlers tackle it, and maybe I found an answer: there is another rule Streams reading "Use streams only for logging". Maybe they just don't use streams in this situation.

lzl124631x
  • 4,485
  • 2
  • 30
  • 49

1 Answers1

5

In order to comply with Google guidelines, change the declaration

void MyTable::LoadTable( ifstream& fin )

to

void MyTable::LoadTable( ifstream* fin )

Now you might be wondering, what's the point of that? And a large part of the answer is probably that the Google C++ style guide simply doesn't make much sense outside of Google. Parts of it can be explained by postulating the need to maintain a large body of C style legacy code, but parts of it are just baffling.

As just one example, std::getline is very much in breach of the Google style guidelines.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Why would you use const & for an input parameter and * for an output parameter rather than & for an output parameter? That's what I find puzzling. – user3344003 Jun 05 '14 at 02:04
  • 4
    @user3344003: One possible explanation is that they want calls to functions which might modify an object to be clear from the caller's side, without having to know what the called function actually does. – Benjamin Lindley Jun 05 '14 at 02:07
  • Personally, I'd convey that in the function name if possible, but I can see that being the reason. – chris Jun 05 '14 at 02:07
  • 1
    @user3344003 Suppose you are deathly afraid someone will `operator=` the data (why else take by reference? And even if it does not, some dastardly villian could change it) without the outside user realizing it: then write a style guide. You get the google rule. The guide looks like ot is written so someone who only understands the intersection of Java and C will not be surprised by the code. – Yakk - Adam Nevraumont Jun 05 '14 at 02:08