diff --git a/example/cpp11/non_def_con_class.cpp b/example/cpp11/non_def_con_class.cpp index 2f53a3a5..0bdb2da7 100644 --- a/example/cpp11/non_def_con_class.cpp +++ b/example/cpp11/non_def_con_class.cpp @@ -23,6 +23,16 @@ struct my { my(int a):a(a) {} int a; MSGPACK_DEFINE(a); + + bool operator==(const my & other) const + { + return other.a == a; + } + + bool operator<(const my & other) const + { + return other.a > a; + } }; namespace msgpack { @@ -48,4 +58,34 @@ int main() { msgpack::object obj(m1, z); std::cout << obj << std::endl; assert(m1.a == obj.as().a); + + // useful keys for maps do not have as<>() method implemented: + std::cout << "has_as " << msgpack::has_as::value << std::endl; + std::cout << "has_as " << msgpack::has_as::value << std::endl; + + // BEFORE PATCH: as a result as >() is not available either. + // AFTER PATCH: as >() is available if key OR value have an as<>() implementation + std::cout << "has_as > " << msgpack::has_as >::value << std::endl; + + std::map m; + m.emplace(1, my(17)); + msgpack::object obj2(m, z); + std::cout << obj2 << std::endl; + + // BEFORE PATCH: this makes the following break with a compiler error, + // beacuse it uses the deleted my:my() constructor, as it falls back to the + // convert() method. + // AFTER PATCH: the following works and uses the customary as() implementation + assert(m == obj2.as()); + + std::map m2; + m2.emplace(17, 42); + msgpack::object obj3(m2, z); + std::cout << obj3 << std::endl; + + // BEFORE PATCH: this makes the following break with a compiler error: + // beacuse it uses the deleted my:my() constructor, as it falls back to the + // convert() method. + // AFTER PATCH: the following works and uses the customary as() implementation + assert(m2 == obj3.as()); }