0

So... I think this leaves a bit of room for interpretation, and I wanted to know if anybody knows what the spirit of the standard is...

class A {
  const int size_;
public:
  A(int size) : size_(size) {}
};
  1. Is the trailing underscore a sign of "private"-ness or "class"-ness?
  2. Is the const field supposed to be "kSize" or "size_"?
  3. If it is moved to be public, should it be "size"?
Kamen
  • 47
  • 7
  • *"I think this leaves a bit of room for interpretation"* -- how so? The first quote says nothing about "private" -- *you* introduced that word without justification. The second quote does not apply to your code, as your code has nothing with static storage duration (you even emphasized this point!). I do see some room for interpretation in the third quote, but that's only because you left out the link that defines what "constant" means in that context (it links to the second quote). So... it appears that all the "room for interpretation" was introduced by you? – JaMiT May 23 '21 at 01:40
  • i should have been clearer... struct fields do not have the trailing "_" and are required to be public. that combined with appended or prepended "_" often meaning private in other languages (e.g. python / dart) make me doubt it. there is something strange looking with a public field that has "_" appended... maybe it is just me but that is why i am asking. – Kamen May 23 '21 at 01:44
  • Here is the vague part about constants in the style (in bold) > All such variables with static storage duration (i.e., statics and globals, see Storage Duration for details) should be named this way. **This convention is optional for variables of other storage classes**, e.g., automatic variables, otherwise the usual variable naming rules apply. from https://google.github.io/styleguide/cppguide.html#Constant_Names – Kamen May 23 '21 at 01:49
  • True, one of the harder skills to employ when reading standards is to not read things that are not there, especially when you've seen something so similar in the past. (By the way, try using backticks to force "code formatting" on your underscores in comments, as in `_`.) – JaMiT May 23 '21 at 01:51

1 Answers1

2

In your example:

class A {
    const int size_;

This member variable is not a "constant" for the purposes of the style guide. Its value cannot be changed after construction, but is different per instance. A "constant" inside a class would be constexpr or static const or enum. As it stands, it is not a constant so does not get a k prefix.

To answer your specific questions individually:

  1. The trailing underscore tells you it's a class member variable.
  2. size_ because it's not a "constant" in terms of the style guide.
  3. Making it public would violate the style guide, so this doesn't really have an answer.

Finally, note that const member variables inhibit the compiler-generated assignment operator, which is one reason you don't see them that often.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • The third bullet I pasted, about access control, directly from the stile guide, suggests that const data members can be declared public... > Make classes' data members private, **unless they are constants**. This simplifies reasoning about invariants, at the cost of some easy boilerplate in the form of accessors (usually const) if necessary. I appreciate your ruling on (2). Does the information above influence your response to (3)? – Kamen May 23 '21 at 01:46
  • oh, i think you are saying that my example member variable is "not a constant according to the style guide", despite the "const" modifier, because the style guide defines constants only as things that remain constant for the entire program? and therefore... (1) you should always put the trailing `_` and (2) you should never have a regular instance class member public (despite it's `const` qualifier)... i think i buy this interpretation, but please confirm i understood you correctly! – Kamen May 23 '21 at 02:01
  • 1
    @Kamen: `const` does not make something a "constant" in terms of the style guide. A "constant" in terms of the style guide is a fixed value, not merely fixed after construction but fixed for the lifetime of the program. So `constexpr` or `static const` members are constants, but `const` are not. So yes you've understood correctly my answer now. – John Zwinck May 23 '21 at 02:01
  • Somebody closed the question stating it is opinion based... oh the irony. Is this an admission that the standard is not well specified? I still like your answer and am going with it. – Kamen May 23 '21 at 21:20