[Boost-bugs] [Boost C++ Libraries] #8268: basic_hold_any missing assignment operator

Subject: [Boost-bugs] [Boost C++ Libraries] #8268: basic_hold_any missing assignment operator
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-03-08 16:05:53


#8268: basic_hold_any missing assignment operator
--------------------------------------------------+-------------------------
 Reporter: Matthew South <msouth@…> | Owner: djowel
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: spirit
  Version: Boost Development Trunk | Severity: Problem
 Keywords: hold_any |
--------------------------------------------------+-------------------------
 Self contatined repro:

 struct Big
 {
    int x[ 4 ];
 }; (big enough to prevent the small optimisation in hold_any)

 {
         boost::spirit::hold_any sourceAny = boost::spirit::hold_any( Big()
 );

         boost::spirit::hold_any copyInto;

         copyInto = sourceAny;

         (calls a compiler generated assignment operator which copies the
 address of the memory allocated in sourceAny)

 } // CRASH: double deletion of the heap allocated Big held in sourceAny as
 both copyInto
 // and sourceAny go out of scope

 The problem is that the assignment operator provided in basic_hold_any
 doesn't suppress the compiler generated one because it is a template
 function, and the compiler generator one only does a shallow copy.

 The relevant section in the C++ standard is N3485 §12.18:

 "A user-declared copy assignment operator X::operator= is a non-static
 non-template member function of class X with exactly one parameter of type
 X, X&, const X&, volatile X& or const volatile X&. (121)"

 "(121) Because a template assignment operator or an assignment operator
 taking an rvalue reference parameter is never a
 copy assignment operator, the presence of such an assignment operator does
 not suppress the implicit declaration of a copy
 assignment operator. Such assignment operators participate in overload
 resolution with other assignment operators, including
 copy assignment operators, and, if selected, will be used to assign an
 object."


 To fix this issue, simply add the non-template assignment constructor to
 basic_hold_any:

 // assignment operator
 basic_hold_any& operator=(basic_hold_any const& x)
 {
     return assign(x);
 }

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/8268>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:12 UTC