This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/56889] =delete(ing) default copy and move operations for a polymorphic type gives compilation error messages


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56889

--- Comment #6 from Amali Praveena <amalisperid at yahoo dot com> 2013-04-09 19:23:38 UTC ---
Hi Daniel,
The Stack template is an abstract type, soÂI don't have to add default
constructor to it, do I?ÂSince the abstract type can't be copied/moved, i'm
explicitly specifying those functions as delete; but the derived classes
(vector_stack) are copyable through clone function(clone pattern).
ÂI tried to do the following (adding default constructor), which still gave me
an error(both options):
template<class T>
class Stack{
ÂÂ public:
ÂÂ // pure virtual member functions
ÂÂ virtual Stack* clone() const=0; // polymorphic
ÂÂ virtual ~Stack() {} // explicitly define a destructor
ÂÂ // no copy operations Bug No:- 56889
ÂÂ Stack() // option 1
ÂÂ {
ÂÂ }
ÂÂ Stack()=default; // option 2
ÂÂ Stack(const Stack&)=delete;
ÂÂ Stack& operator=(const Stack&)=delete;
ÂÂ //no move operations
ÂÂ Stack(Stack&&)=delete;
ÂÂ Stack& operator=(Stack&&)=delete;
ÂÂ virtual bool empty() const=0; // const member function
ÂÂ virtual std::size_t size() const=0; // const member function
ÂÂ virtual T& top()=0;
ÂÂ virtual const T& top() const=0; // const member function
ÂÂ virtual void push(const T& x)=0;
ÂÂ virtual void pop()=0;
};
Here is a section from Bjarne stroustrup's PL fourth edition draft version :
Preventing Copy and Move [tour2.copy.hier]
Using the default copy or move for a class in a hierarchy is typically a
disaster: Given only
a pointer to a base, we simply donât know what members the derived class has
(Â3.3.3), so
we canât know how to copy them. So, the best thing to do is usually to delete
the default
copy and move operations; that is, to eliminate to default deïnitions of those
two operations:
class Shape {
public:
Sha pe(const Sha pe&) =delete; // no copy operations
Sha pe& opera tor=(const Sha pe&) =delete;
Sha pe(Sha pe&&) =delete; // no move operations
Sha pe& opera tor=(Shape&&) =delete;
ËSha pe();
// ...
};
Now an attempt to copy a Sha pe will be caught by the compiler. If you need to
copy an
object in a class hierarchy, write some kind of clone function (Â22.2.4).
In case you forgot to delete a copy or move operation, no harm is done. A move
operation
is not implicitly generated for a class where the user has explicitly declared
a destructor.
Furthermore, the generation of copy operations are deprecated in this case
(Â42.2.3). This can be a good reason to explicitly deïne a destructor even
where the compiler
would have implicitly provided one (Â17.2.3).
A base class in a class hierarchy is just one example of an object we wouldnât
want to
copy.
The C++ Programming Language, 4th edition Â2012 by Pearson Education, Inc.
Reproduced in draft form with the permission of the publisher. D R A F T
thanks,
Amali.


________________________________
 From: daniel.kruegler at googlemail dot com <gcc-bugzilla@gcc.gnu.org>
To: amalisperid@yahoo.com 
Sent: Tuesday, 9 April 2013 7:10 PM
Subject: [Bug c++/56889] =delete(ing) default copy and move operations for a
polymorphic type gives compilation error messages


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56889

Daniel KrÃgler <daniel.kruegler at googlemail dot com> changed:

      What  |Removed           |Added
----------------------------------------------------------------------------
         CC|              |daniel.kruegler at
          |              |googlemail dot com

--- Comment #1 from Daniel KrÃgler <daniel.kruegler at googlemail dot com>
2013-04-09 09:10:52 UTC ---
There are several problems in your example:

1) You have not declared a default constructor in template Stack, but you have
provided user-declared constructors (The deleted ones). This has the effect
that the Stack template has a no implicitly declared default constructor, which
again has the effect that the initializer-list constructor generated by the
compiler in vector_stack is deleted, because it would implicitly call the
missing default constructor of Stack. Solution: Add a (defaulted) default
constructor to the Stack template.

2) The clone function in vector_stack would call the copy constructor of that
type. But this one is deleted, because the Stack template has an explicitly
deleted copy constructor. This is a design error.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]