Bug 34124 - auto_ptr ambiguous conversion
Summary: auto_ptr ambiguous conversion
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 34125 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-11-16 19:55 UTC by Csaba Fekete
Modified: 2008-01-25 20:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Csaba Fekete 2007-11-16 19:55:12 UTC
Hi,

Running the following code:

#include <memory>

using namespace std;
  
class CBase {};
class CDerived : public CBase {};

void Set(auto_ptr<CBase> base)
{
}

int main(int argc, char *argv[])
{
  auto_ptr<CDerived> derived(new CDerived);

  Set(derived); // error: conversion from 'std::auto_ptr<CDerived>' to 'std::auto_ptr<CBase>' is ambiguous

  auto_ptr<CBase> base(derived); // but this is OK
  base = derived; // this is OK too
}

the Set(derived) gives the mentioned error in the comment. The next two lines compiles without error so both the copy constructor and the operator= of auto_ptr works fine. But for some reason the conversion is ambiguous if a function (in this case Set) is called.

In the next two lines of the compiler output, there are two notes:
/usr/include/c++/4.2.1/memory:368: note: candidates are: std::auto_ptr<_Tp>::operator std::auto_ptr<_Tp1>() [with _Tp1 = CBase, _Tp = CDerived]
/usr/include/c++/4.2.1/memory:212: note: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = CDerived, _Tp = CBase]

Please could you check if this is a bug or I made something wrong?

Thanks,
Csaba
Comment 1 Paolo Carlini 2007-11-16 20:26:08 UTC
*** Bug 34125 has been marked as a duplicate of this bug. ***
Comment 2 Paolo Carlini 2008-01-25 20:31:10 UTC
There isn't much to say: both the templated constructor and the templated conversion operator must be there per the current C++ standard, and your code cannot compile. Note that, in general, the design of auto_ptr is considered seriously broken and therefore it will be deprecated for the next standard and unique_ptr provided as replacement.