[llvm-bugs] [Bug 48598] New: Incorrect overload resolution when performing direct initialization

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Dec 25 07:35:46 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48598

            Bug ID: 48598
           Summary: Incorrect overload resolution when performing direct
                    initialization
           Product: clang
           Version: 11.0
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++17
          Assignee: unassignedclangbugs at nondot.org
          Reporter: karzh at mail.ru
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Consider direct initialization of an object of type B with an lvalue of type A
as an initializer. The following example compiles successfully as C++14 but
fails to compile as C++17 (https://godbolt.org/z/dhs3Wv):
---------------------------
struct A;

struct B
{
    B(const A&) { }
    B(const B&) { }
};

struct A
{
    operator B () = delete;
};

int main()
{
    A a;
    B&& b = B(a);
}
---------------------------

In C++17 mode clang reports the access to deleted operator and presents a list
of candidate functions:
---------------------------
<source>:17:13: error: functional-style cast from 'A' to 'B' uses deleted
function
    B&& b = B(a);
            ^~~
<source>:11:5: note: candidate function has been explicitly deleted
    operator B () = delete;
    ^
<source>:5:5: note: candidate constructor
    B(const A&) { }
    ^
<source>:6:5: note: candidate constructor
    B(const B&) { }
    ^
---------------------------

According to [over.match.ctor]:
"When objects of class type are direct-initialized, ..., overload resolution
selects the constructor. For direct-initialization ... the candidate functions
are all the constructors of the class of the object being initialized".

Therefore, the operator in the above example should not be included in the
candidate list. When considering constructors, the first one (taking const A&)
should win, since it doesn't require user-defined conversion.

A similar problem occurs when using an rvalue of type A as an initializer.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201225/bfebdeab/attachment.html>


More information about the llvm-bugs mailing list