[Boost-bugs] [Boost C++ Libraries] #3977: Compile error on make_nvp with reference data member

Subject: [Boost-bugs] [Boost C++ Libraries] #3977: Compile error on make_nvp with reference data member
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-03-03 22:36:51


#3977: Compile error on make_nvp with reference data member
---------------------------------+------------------------------------------
 Reporter: kondo@… | Owner:
     Type: Patches | Status: new
Milestone: Boost 1.43.0 | Component: None
  Version: Boost 1.42.0 | Severity: Problem
 Keywords: |
---------------------------------+------------------------------------------
 When I save the reference data member to the xml_archive by using
 make_nvp, the compile error occurs.

 Attached file 'ref_direct.cpp' is an example program to reproduce problem.


 {{{
 #!cpp
     // Compile error!
     ar << boost::serialization::make_nvp("ref_", &p->ref_);
 }}}

 The error message is below.
 {{{
 error: invalid initialization of non-const reference of type ‘MyInt*&’
 from a temporary of type ‘MyInt*'
 }}}

 We can avoid this error using additional variable or using static_cast.

 {{{
 #!cpp
     // OK
     MyInt* pRef = &p->ref_;
     ar << boost::serialization::make_nvp("ref_", pRef);
 }}}

 {{{
 #!cpp
     // OK
     ar << boost::serialization::make_nvp("ref_", static_cast<const MyInt *
 const &>(&p->ref_));
 }}}

 But I think it's not elegant.

 My idea is that introducing the new overload of make_nvp function
 template.

 {{{
 #!cpp
 template<class T>
 inline
 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
 const
 #endif
 nvp<const T> make_nvp(const char * name, const T & t){
     return nvp<const T>(name, t);
 }
 }}}

 It resolves this problem.

 Please check 'nvp_const_ref.patch' (attached file).

 After apply the patch, I already did the regression test of serialization.
 All test is passed.

 My testing environments are
 g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1 and
 VC++ 9.0 on Windows7.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3977>
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:02 UTC