Axter said:
Kai-Uwe Bux said:
Axter said:
(e-mail address removed) wrote:
BTW, do not use the standard library auto_ptr. Due to it's copy
semantics, it is not appropriate for use in STL containers.
Any fully compliant C++ compiler will not allow you to do a push_back
to a container of std::vector<auto_ptr<T> > type.
Where in the standard did you find that? The closest I found is
[20.4.5/3]:
[...] auto_ptr does not meet the CopyConstructible and Assignable
requirements for Standard Library container elements and thus
instantiating a Standard Library container with an auto_ptr results
in undefined behavior.
This quote says it's undefined behavior. You seem to claim that it is an
error and diagnostics is required.
[snip]
But it does disallow it indirectly, since the standard clearly defines
an auto_ptr copy constructor taking a non-constant type, and a
vector:

ush_back taking a constant type.
20.4.5.1 auto_ptr constructors
auto_ptr(auto_ptr &a) throw();
//23.2.4.3 modifiers
void push_back(const T& x);
So IAW C++ standard you can not perform a push_back of type auto_ptr<T>
on a container of auto_ptr<T>.
IAW C++ standard, your compiler may or may not let you declare a
container of auto_ptr<T>, but it can not let you populate it via
push_back.
Hm, I think you are reading too much into the signatures. Consider the
following (absolutely unlikely and bad) implementation of std::vector:
#include <cstddef>
#include <memory>
#include <cassert>
namespace std {
template < typename T, typename A = std::allocator<T> >
struct vector {
typedef std::size_t size_type;
typedef T value_type;
typedef T * pointer;
typedef T const * const_pointer;
typedef T & reference;
typedef T const & const_reference;
private:
pointer the_data;
size_type the_size;
size_type the_cap;
public:
// ...
void push_back ( const_reference c_ref ) {
if ( the_size == the_cap ) {
// do some fancy reallocation stuff
}
assert( the_size < the_cap );
new ( the_data + the_size )
value_type ( const_cast<reference>( c_ref ) );
++ the_size;
}
}; // vector
} // namespace std
I would claim that this implementation is conforming:
a) it has the right signature.
b) for any type T that satisfies the conceptual requirements (like
CopyConstructible), it does the right thing.
This implementation will, however, not trigger an error message for
std::auto_ptr.
As a practical matter, of course, you are right: it is more than likely that
a compiler will give you an error. But I do not see why it would be
required to do so by the standard.
Best
Kai-Uwe Bux