-3

If I'm not wrong, these function parameters:

void foo(const Type& type);
void foo(Type const& type);

are equivalent. What is the standard way to declare them? By using the first or the second function declaration? With "standard" I mean the most readable and used version, if ISO C++ does not define any spec.

ABCplus
  • 3,981
  • 3
  • 27
  • 43
  • Where have you seen the second form being used ? Almost all places use the first format. – DumbCoder May 27 '15 at 13:44
  • @DumbCoder the second form is often used, since it is consistent with `typedef int* PTRINT`s for pointers: although `const PTRINT` and `PTRINT const` both declare a `const` pointer, using the second form makes it feel almost like a macro, where if you blindly substitute you get the correct declaration. – vsoftco May 27 '15 at 13:51
  • I don't understand the question. As you say, the standard defines both to be equivalent. Therefore, both are standard. – Mike Seymour May 27 '15 at 13:55
  • The question has been marked as duplicate, but I'm not questioning about what is the difference between the two declarations, instead I'm asking what is the standard way to declare that function, the most readable and used version since I KNOW they are equivalent. – ABCplus May 27 '15 at 14:04
  • I re-opened, but this is basically opinion based, because when things are equivalent, there is no real "standard". – juanchopanza May 27 '15 at 14:08
  • If there's no real "standard" so an acceptable answer would be: there's no real "standard" – ABCplus May 27 '15 at 14:09
  • "With "standard" I mean the most readable and used version": most readable would be a matter of opinion, and most used would require trawling through a large and representative sample from all the C++ code available. – juanchopanza May 27 '15 at 14:14
  • I understand your point of view. I'm simply asking if this or other typical C++ declaration have never been standardized for the sake of clearity and readability. – ABCplus May 27 '15 at 14:24

2 Answers2

1

What is the standard way to declare them?

Both are standard.

With "standard" I mean the most readable and used version

Readability is in the eye of the beholder. I prefer the second, since it gives a consistent right-to-left ordering. Some prefer the first, since it unambiguously associates the qualifier with the type it qualifies.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Has ISO C++ or some other committee ever "standardized" these declarations? Some sort of coding guidelines to uniform the source codes. – ABCplus May 27 '15 at 14:27
  • 2
    @ABCplus: No, ISO only specify how the language works, and there's no central committee of C++ style. – Mike Seymour May 27 '15 at 14:28
0

There is no commonly-accepted C++ style guide. The closest I can think of is Google's C++ guide and what they have to say on the subject is...

Some people favor the form int const foo to const int foo. They argue that this is more readable because it's more consistent: it keeps the rule that const always follows the object it's describing. However, this consistency argument doesn't apply in codebases with few deeply-nested pointer expressions since most const expressions have only one const, and it applies to the underlying value. In such cases, there's no consistency to maintain. Putting the const first is arguably more readable, since it follows English in putting the "adjective" (const) before the "noun" (int).

That said, while we encourage putting const first, we do not require it. But be consistent with the code around you!

Community
  • 1
  • 1
QuestionC
  • 10,006
  • 4
  • 26
  • 44