Topic: Reference binding, rvalues and implicit conversions


Author: RaoulGough@yahoo.co.uk
Date: 1 Jun 2005 16:00:02 GMT
Raw View
With recent g++ (since about 3.3.4) and also current Comeau online, the
following code doesn't compile:

#include <memory>

void foo1 (std::auto_ptr<int> const &); // Problem

void bar() {
  // Fails - binding auto_ptr rvalue to reference parameter
  foo1 (std::auto_ptr<int>());
}

It looks like the compilers aren't considering the implicit conversion
to auto_ptr_ref<int> when determining if the binding is valid under
8.5.3/5. Is this some consequence of TR1? Presumably the resolution to
core issue 391 will make this code valid?

Here's a more complete example, with compiler error message. Note that
pass by value does work, as does implicit construction from an
auto_ptr_ref:

$ cat ref_conversion.cpp
#include <memory>

void foo1 (std::auto_ptr<int> const &); // Problem
void foo2 (std::auto_ptr<int>);         // OK

void bar() {
  // Fails - binding auto_ptr rvalue to reference parameter
  foo1 (std::auto_ptr<int>());

  // OK - implicit construction from auto_ptr_ref<int>
  foo1 (static_cast<std::auto_ptr_ref<int> >(
            std::auto_ptr<int>()));

  // OK - binding auto_ptr rvalue to value parameter
  foo2 (std::auto_ptr<int>());
}

$ g++ -o ref_conversion.o -c ref_conversion.cpp
ref_conversion.cpp: In function 'void bar()':
ref_conversion.cpp:8: error: no matching function for call to
'std::auto_ptr<int>::auto_ptr(std::auto_ptr<int>)'
./include/c++/4.0.0/memory:349: note: candidates are:
  std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = int]
./include/c++/4.0.0/memory:212: note:
  std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = int,
_Tp = int]
./include/c++/4.0.0/memory:199: note:
  std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = int]

$ g++ --version
g++ (GCC) 4.0.0

--
Raoul Gough.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: squell@alumina.nl (Marc Schoolderman)
Date: Mon, 6 Jun 2005 21:17:26 GMT
Raw View
RaoulGough@yahoo.co.uk wrote:

> It looks like the compilers aren't considering the implicit conversion
> to auto_ptr_ref<int> when determining if the binding is valid under
> 8.5.3/5. Is this some consequence of TR1? Presumably the resolution to
> core issue 391 will make this code valid?

Intel C++'s error message on this may be helpful, I think:

ref.cpp(8): warning #377: class "std::auto_ptr<int>" has no suitable
copy constructor
     foo1 (std::auto_ptr<int>());
           ^

And the resolution for issue 391 scraps the requirement for a copy
constructor in this case. This is an odd situation; the fact that the
expression is reference compatible, actively hinders reference binding.

Consider this simplification of your code, eliminating auto_ptr<>;

$ cat test.cpp
struct foo {
     foo();
private:
     foo(const foo&);
};

void baz(const foo&);

int main()
{
     baz( foo() );
}

$ icc -c test.cpp
test.cpp(11): warning #734: "foo::foo(const foo &)", required for copy
that was eliminated, is inaccessible
       baz( foo() );
            ^

~Marc

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]