Skip to content

Add brace initialisers to coding standard #3546

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

Conversation

hannes-steffenhagen-diffblue
Copy link
Contributor

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

Major benefit:

  • C++ standard requires a diagnostic on narrow conversions in brace initialisers, e.g.
#include <iostream>

struct X {
  char x; 
  X(char x) : x(x) {}
};

struct Y {
  float x;
  Y(float x) : x(x) {}
};

int main()
{
  int val = 10;
  // may or may not warn (does not by default in g++)
  auto const a = X(val);
  // diagnostic required by C++ standard!
  auto const b = X{val};

  double dval = 3.0;
  // again, no warning with g++
  auto const c = Y(dval);
  // diagnostic required
  auto const d = Y{dval};
}

Minor benefits:

  • Can visually distinguish constructor from function calls
  • Can use slightly more consistent syntax type_name variable_name{}; instead of type_name variable_name; (parentheses version doesn't work because it's indistinguishable from a function declaration)

Copy link
Collaborator

@tautschnig tautschnig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm largely a "don't care" on this one - I find it visually displeasing, but that's probably just because I'm not yet used to it. So I'll have someone else approve. (The major benefit mentioned in the PR description I'd just comment on with "use explicit.")

@@ -209,6 +209,8 @@ Formatting is enforced using clang-format. For more information about this, see
point to heap-allocated memory should be private data members of an object
which safely manages the pointer. As such, `new` should only be used in
constructors, and `delete` in destructors. Never use `malloc` or `free`.
- Prefer brace style initialisation (i.e. `type_name{arguments...}`) over
parentheses for constructor calls
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parentheses for constructor calls
parentheses for constructor calls.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫
This PR failed Diffblue compatibility checks (cbmc commit: 38b1b98).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/94044400
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.

Common spurious failures:

  • the cbmc commit has disappeared in the mean time (e.g. in a force-push)
  • the author is not in the list of contributors (e.g. first-time contributors).

The incompatibility may have been introduced by an earlier PR. In that case merging this
PR should be avoided unless it fixes the current incompatibility.

@hanst99
Copy link
Contributor

hanst99 commented Dec 8, 2018

This is also in CPPCoreGuidelines, which we obviously don't follow atm but I feel like if we don't have a particular preference either way going with those is a good start. They also have more detailed reasoning for their recommendations most of the time.

Copy link
Contributor

@NlightNFotis NlightNFotis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm all for it, especially for the benefit of not allowing implicit narrowing conversions.

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

Successfully merging this pull request may close these issues.

6 participants