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 definitions 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 define 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-bugzi...@gcc.gnu.org> To: amalispe...@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.