Re: [Boost-users] serialize explicit template instantiation

This email was an eye popper for me because I have never had any luck declaring a template function in a header file and defining the implementation in a cpp file. I either get compilation/link errors, or very odd run-time problems. My understanding is that many compilers (of which mine, the MSVC 7.1 compiler) simply cannot handle this situation at all. I have always had to include the actual implementation of the template function inside the header file, which is a real pain. Note that this has nothing to do with the serialization per se. Are other people finding that newer compilers are finally able to handle this? I have several projects that I could improve by splitting the implementation code away from the headers. Ed -----Original Message----- From: Robert Ramey [mailto:ramey@rrsd.com] Sent: Wednesday, November 29, 2006 8:33 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] serialize explicit template instantiation I just compiled, linked and ran the following program on my system: vc 7.1 Boost 1.35 - HEAD There was no need to explicitly instantiate anything. Robert Ramey #include <boost/serialization/string.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/version.hpp> class CSerTest { std::string a_, b_, c_; friend class boost::serialization::access; template<class Archive> void serialize( Archive & ar, const unsigned int ); public: CSerTest(); }; BOOST_CLASS_VERSION(CSerTest, 1); // Class.cpp #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> CSerTest::CSerTest() : a_("1"), b_("2"), c_("3") { } template<class Archive> void CSerTest::serialize( Archive & ar, const unsigned int ) { ar & BOOST_SERIALIZATION_NVP(a_) & BOOST_SERIALIZATION_NVP(b_) & BOOST_SERIALIZATION_NVP(c_); } // main.cpp #include <fstream> ///////////////////////// without effect //////////////////////////////////// //template void CSerTest::serialize<boost::archive::xml_oarchive>(boost::archive::xml_oa rchive&, unsigned int ); int main() { const CSerTest instance; std::ofstream ofs( "out_file.txt" ); boost::archive::xml_oarchive oxa( ofs ); oxa << BOOST_SERIALIZATION_NVP(instance); } The information contained in or attached to this email may be subject to the Export Administration Regulations (EAR), administered by the U.S. Department of Commerce, or the International Traffic in Arms Regulations (ITAR), administered by the U.S. Department of State, and may require an export license from the Commerce or State Department prior to its export. An export can include a release or disclosure to a foreign national inside or outside the United States. Include this notice with any reproduced portion of this information.

Reusser, Edward wrote:
This email was an eye popper for me because I have never had any luck declaring a template function in a header file and defining the implementation in a cpp file. I either get compilation/link errors, or very odd run-time problems. My understanding is that many compilers (of which mine, the MSVC 7.1 compiler) simply cannot handle this situation at all. I have always had to include the actual implementation of the template function inside the header file, which is a real pain.
What has worked well for me is the following: a) declare the template in the header. b) instantiate it in a *.cpp file Works - but can lead to multiply defined symbols a link time. So I use: a) declare template in header. b) create "implementation" *.ipp file e) create *.cpp which i) includes the *.ipp file ii) explicitly instantiate the template. This is used to add precompiled instantiations to the serialization libraries. The serialiation library uses this for all compilers going back to msvc 6.0 and borland 5.51. Robert Ramey

a) declare template in header. b) create "implementation" *.ipp file e) create *.cpp which i) includes the *.ipp file ii) explicitly instantiate the template. This is used to add precompiled instantiations to the serialization libraries.
how can you istantiate the template if you don't have the concrete type yet? ... or have I missed something obvious? thanks for your info, Marco
participants (3)
-
Marco De Paoli
-
Reusser, Edward
-
Robert Ramey