[LLVMbugs] [Bug 17295] New: Calling make_shared<int> with a unique_ptr<int>&& should not compile

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Sep 19 15:06:06 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=17295

            Bug ID: 17295
           Summary: Calling make_shared<int> with a unique_ptr<int>&&
                    should not compile
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ali.baharev at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

The following code compiles without any warning.

#include <iostream>
#include <memory>

int main()
{
   std::unique_ptr<int> u_ptr(new int(42));

   std::cout << " u_ptr.get() = " <<  u_ptr.get() << std::endl;
   std::cout << "*u_ptr       = " << *u_ptr       << std::endl;

   auto s_ptr = std::make_shared<int>(std::move(u_ptr));

   std::cout << "After move" << std::endl;

   std::cout << " u_ptr.get() = " <<  u_ptr.get() << std::endl;
   std::cout << "*u_ptr       = " << *u_ptr       << std::endl;
   std::cout << " s_ptr.get() = " <<  s_ptr.get() << std::endl;
   std::cout << "*s_ptr       = " << *s_ptr       << std::endl;
}

It prints:

 u_ptr.get() = 0x1a5c010
*u_ptr       = 42
After move
 u_ptr.get() = 0x1a5c010
*u_ptr       = 42
 s_ptr.get() = 0x1a5c048
*s_ptr       = 1

I believe the above code should not compile, int doesn't have ctor taking 
unique_ptr<int>&& as an argument. GCC 4.7.2 rejects the above code with the
following error:

/usr/include/c++/4.7/ext/new_allocator.h:110:4: error: invalid user-defined
conversion from ‘std::unique_ptr<int>’ to ‘int’ [-fpermissive]
In file included from /usr/include/c++/4.7/memory:86:0,
                 from ptr.cpp:2:
/usr/include/c++/4.7/bits/unique_ptr.h:233:16: note: candidate is:
std::unique_ptr<_Tp, _Dp>::operator bool() const [with _Tp = int; _Dp =
std::default_delete<int>] <near match>
/usr/include/c++/4.7/bits/unique_ptr.h:233:16: note:   return type ‘bool’ of
explicit conversion function cannot be converted to ‘int’ with a qualification
conversion

-- 
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/20130919/1c65dcf8/attachment.html>


More information about the llvm-bugs mailing list