Serialization--a peculiar observation regarding operator<<

The test program below (under boost 1_33, vc71) compiles fine, unless I
change the "#if 1" to "#if 0", in which case it produces this:
error C2679: binary '<<': no operator found which takes a right-hand
operand of type 'double' (or there is no acceptable conversion)
Can anyone explain why that should happen? A double is a double--why
should '<<' care whether the rhs comes from funcall a variable?
Note: The test program is clearly a nonsense program (in real life the
role of foo() is played by some public (get-accessor) on klass--a very
common pattern when doing a non-intrusive serialization). But I did not
want to cloud the issue--I wanted to emphasize that the bug, if it is a
bug, seems to have nothing to with klass.
Thank you.
Michael Drumheller
-----------------------------------------------------------------begin--
-------------------------------------------------------------
#include <fstream>
#include

Try changing the return type of foo() to const double. For the reason see the ratinale page in the serialization documentation. Oliver Drumheller, Michael wrote:
The test program below (under boost 1_33, vc71) compiles fine, unless I change the "#if 1" to "#if 0", in which case it produces this: error C2679: binary '<<': no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
Can anyone explain why that should happen? A double is a double--why should '<<' care whether the rhs comes from funcall a variable?
Note: The test program is clearly a nonsense program (in real life the role of foo() is played by some public (get-accessor) on klass--a very common pattern when doing a non-intrusive serialization). But I did not want to cloud the issue--I wanted to emphasize that the bug, if it is a bug, seems to have nothing to with klass.
Thank you.
Michael Drumheller -----------------------------------------------------------------begin-- ------------------------------------------------------------- #include <fstream> #include
#include class klass {}; double foo() { return 3.1416; } namespace boost { namespace serialization { template<class Archive> void save(Archive &ar, const klass & b, const unsigned int ver) { #if 1 double x=foo(); ar << x; #else ar << foo(); #endif } template<class Archive> void load(Archive &ar, klass & b, const unsigned int ver){}//N/A for this demo }} BOOST_SERIALIZATION_SPLIT_FREE(klass); void out(const char *testfile, const klass & c) { std::ofstream ofs(testfile); boost::archive::text_oarchive oa(ofs); oa << c; } ----------------------------------------------------------------end----- ----------------------------------------------------------------- Michael Drumheller Boeing Phantom Works Mathematics and Engineering Analysis 425.865.3520 michael.drumheller@boeing.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

hmm The operator ar << takes a reference to a const argument. It looks like the compiler is objecting to taking the reference to an argument on the stack ar << foo(); which seems like a good idea to me. In this case there would be no problem, but in the case where the return of foo() would be a "tracked" type, this would break the serialization system. Robert Ramey Drumheller, Michael wrote:
The test program below (under boost 1_33, vc71) compiles fine, unless I change the "#if 1" to "#if 0", in which case it produces this: error C2679: binary '<<': no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
Can anyone explain why that should happen? A double is a double--why should '<<' care whether the rhs comes from funcall a variable?
Note: The test program is clearly a nonsense program (in real life the role of foo() is played by some public (get-accessor) on klass--a very common pattern when doing a non-intrusive serialization). But I did not want to cloud the issue--I wanted to emphasize that the bug, if it is a bug, seems to have nothing to with klass.
Thank you.
Michael Drumheller -----------------------------------------------------------------begin-- ------------------------------------------------------------- #include <fstream> #include
#include class klass {}; double foo() { return 3.1416; } namespace boost { namespace serialization { template<class Archive> void save(Archive &ar, const klass & b, const unsigned int ver) { #if 1 double x=foo(); ar << x; #else ar << foo(); #endif } template<class Archive> void load(Archive &ar, klass & b, const unsigned int ver){}//N/A for this demo }} BOOST_SERIALIZATION_SPLIT_FREE(klass); void out(const char *testfile, const klass & c) { std::ofstream ofs(testfile); boost::archive::text_oarchive oa(ofs); oa << c; } ----------------------------------------------------------------end----- ----------------------------------------------------------------- Michael Drumheller Boeing Phantom Works Mathematics and Engineering Analysis 425.865.3520 michael.drumheller@boeing.com
participants (3)
-
Drumheller, Michael
-
Oliver Mutz
-
Robert Ramey