[PATCH] D30547: [clang-tidy] Forwarding reference overload in constructors

AndrĂ¡s Leitereg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 2 12:17:20 PST 2017


leanil created this revision.
leanil added a project: clang-tools-extra.
Herald added subscribers: JDevlieghere, mgorny.

Perfect forwarding constructors are called instead of copy constructors if the forwarding reference provides a closer match (e.g. with non-const parameter). This can be confusing to developers unfamiliar with this feature.

  class Person {
  public:
    // perfect forwarding ctor
    template<typename T>
    explicit Person(T&& n) {}
  
    // (possibly compiler generated) copy ctor
    Person(const Person& rhs); 
  };

For detailed explanation of the issue, see Scott Meyers' Effective Modern C++, Item 26.

Running the checker on the LLVM source produces warnings for the following constructors:

https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/iterator.h#L287
https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/iterator_range.h#L39
https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Support/Allocator.h#L150

These are probably not errors, but the described issue does occur (i.e. the perfect forwarding constructor can be called instead of the default copy), so it may worth a double-check.


https://reviews.llvm.org/D30547

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/ForwardingReferenceOverloadCheck.cpp
  clang-tidy/misc/ForwardingReferenceOverloadCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-forwarding-reference-overload.rst
  test/clang-tidy/misc-forwarding-reference-overload.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30547.90367.patch
Type: text/x-patch
Size: 13334 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170302/ada96e8e/attachment.bin>


More information about the cfe-commits mailing list