Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Template type conversion operator not used leading to erroneous compilation error #21771

Open Quuxplusone opened 9 years ago

Quuxplusone commented 9 years ago
Bugzilla Link PR21772
Status NEW
Importance P normal
Reported by Yasir Assam (mail@endlessvoid.com)
Reported on 2014-12-05 23:14:25 -0800
Last modified on 2015-01-13 18:34:54 -0800
Version 3.5
Hardware PC Linux
CC dgregor@apple.com, llvm-bugs@lists.llvm.org, tavianator@tavianator.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
The following code looks like valid C++ 11 but fails with error

"no matching constructor for initialization of 'Ptr<Shape>'"

It compiles ok on gcc 4.9. Based on example code taken from Stroustrup's The
C++ Programming Language 4th edition, pp 764-765.

template <typename T>
struct Ptr {
    Ptr(T* pp) : p{pp} { }

    template <typename U>
    explicit operator Ptr<U>(); // Convert Ptr<T> to Ptr<U>
private:
    T* p;
};

template<typename T>
template<typename U>
Ptr<T>::operator Ptr<U>()
{
    return Ptr<U>{p};
}

struct Shape {};
struct Circle : Shape {};

int main()
{
    Circle c;
    Ptr<Circle> pc{&c};
    // The following instantiation causes the
    // error. Ptr<Circle>::operator Ptr<Shape>() should be getting
    // implicitly called on the argument pc, before the result is used
    // to call Ptr<Shape>'s default copy constructor.
    Ptr<Shape> ps{pc};
}
Quuxplusone commented 9 years ago

_Bug 21991 has been marked as a duplicate of this bug._