Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constness of parameters in function definitions and declarations #1155

Open
eggrobin opened this issue Nov 26, 2016 · 0 comments
Open

Constness of parameters in function definitions and declarations #1155

eggrobin opened this issue Nov 26, 2016 · 0 comments

Comments

@eggrobin
Copy link
Member

It is of no concern to the caller whether a non-reference parameter is const or not. This matters in the function definition (it has to be non-const when, e.g., moving, or using the parameter as a local variable, and should be const when that is not required), but is useless clutter in the declaration; the compiler does not check consistency between the declaration and the definition, and even if consistency is maintained, this is an implementation detail.

Decision.
Definition (const pointer declarator).

A ptr-declarator is const pointer declarator if it is of any of the following forms:

  • * [attribute-specifier-seq] [cv-qualifier-seq] declarator-id [attribute-specifier-seq], where the cv-qualifier-seq contains const;
  • ptr-operator ptr-declarator, where the ptr-declarator is a const pointer declarator;
  • a noptr-declarator of any of the following forms:
    • noptr-declarator [[constant-expression]][attribute-specifier-seq], where the noptr-declarator is a const pointer declarator;
    • noptr-declarator parameters-and-qualifiers, where the noptr-declarator is a const pointer declarator;
    • ( ptr-declarator ), where the ptr-declarator is a const pointer declarator.
Definition (const pointer abstract declarator).

A ptr-abstract-declarator is const pointer abstract declarator if it is of any of the following forms:

  • * [attribute-specifier-seq] [cv-qualifier-seq], where the cv-qualifier-seq does contains const;
  • ptr-operator ptr-abstract-declarator, where the ptr-abstract-declarator is a const pointer abstract declarator;
  • a noptr-abstract-declarator of any of the following forms:
    • noptr-abstract-declarator [[constant-expression]][attribute-specifier-seq], where the noptr-abstract-declarator is a const pointer abstract declarator;
    • noptr-abstract-declarator parameters-and-qualifiers, where the noptr-abstract-declarator is a const pointer abstract declarator;
    • ( ptr-abtract-declarator ), where the ptr-abstract-declarator is a const pointer abstract declarator.

In the parameter-declaration-list of a function declarator that is not the declarator of a function-definition,

  • for parameter-declarations with a declarator which is a ptr-declarator, that ptr-declarator shall not be a const pointer declarator; further, if the declarator is of the form declarator-id [attribute-specifier-seq], the decl-specifier-seq of the parameter-declaration shall not contain const as one of its decl_specifiers.
  • for parameter-declarations with an abstract-declarator which is a ptr-abstract-declarator, that ptr-abstract-declarator shall not be a const pointer abstract declarator
  • for parameter-declarations which do not have a declarator or an abstract-declarator, the decl-specifier-seq of the parameter-declaration shall not contain const as one of its decl_specifiers.

In other words,

void foo(int x);  // OK.
void foo(int);  // OK (though see the styleguide recommendations on
                // naming parameters in declarations).
void foo(int const);  // No.
void foo(int const x);  // No.
void foo(int const* x);  // OK.
void foo(int const& x);  // OK.
void foo(int* const x);  // No.
void foo(int (* const x)());  // No. Really?
void foo(int (*x)());  // You may want a type alias, but OK.
void foo(int const x[5]);  // OK.
void foo(int const (* const x)[5]);  // No!
void foo(int const (* const x())());  // Just say no.
void foo(auto x() -> int);  // OK.

In the function definitions, const as much as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant