brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.1 KiB · 1bef268 Raw
57 lines · plain
1.. title:: clang-tidy - google-explicit-constructor2 3google-explicit-constructor4===========================5 6 7Checks that constructors callable with a single argument and conversion8operators are marked explicit to avoid the risk of unintentional implicit9conversions.10 11Consider this example:12 13.. code-block:: c++14 15  struct S {16    int x;17    operator bool() const { return true; }18  };19 20  bool f() {21    S a{1};22    S b{2};23    return a == b;24  }25 26The function will return ``true``, since the objects are implicitly converted27to ``bool`` before comparison, which is unlikely to be the intent.28 29The check will suggest inserting ``explicit`` before the constructor or30conversion operator declaration. However, copy and move constructors should not31be explicit, as well as constructors taking a single ``initializer_list``32argument.33 34This code:35 36.. code-block:: c++37 38  struct S {39    S(int a);40    explicit S(const S&);41    operator bool() const;42    ...43 44will become45 46.. code-block:: c++47 48  struct S {49    explicit S(int a);50    S(const S&);51    explicit operator bool() const;52    ...53 54 55 56See https://google.github.io/styleguide/cppguide.html#Explicit_Constructors57