From: David Reiss Date: Wed, 27 Feb 2008 19:38:51 +0000 (+0000) Subject: Add an operator< for Thrift structs (in C++). X-Git-Tag: 0.2.0~945 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=cf2e8d9af3650212ed02d61e3f21c4eb29c6ffa4;p=common%2Fthrift.git Add an operator< for Thrift structs (in C++). Some people want to use sets of Thrift structs. This has interesting implications, but it is a reasonable request. However, in C++, this requires structures to have a less-than operator. It seems a little dangerous to auto-generate an arbitrary comparator, but allowing users to define their own operator< implementations seems fine. This change makes that a lot easier. The one downside of this change is that developers who try to compare structures with operator< (including trying to make sets of them) will now get a linker error instead of a compiler error. However, the old compiler error was so scary that I'm not sure this is any worse. Reviewed By: kholst, mcslee Test Plan: make check git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665527 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc index 93ad7eed..c7df0eef 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -770,6 +770,13 @@ void t_cpp_generator::generate_struct_definition(ofstream& out, indent() << "bool operator != (const " << tstruct->get_name() << " &rhs) const {" << endl << indent() << " return !(*this == rhs);" << endl << indent() << "}" << endl << endl; + + // Generate the declaration of a less-than operator. This must be + // implemented by the application developer if they wish to use it. (They + // will get a link error if they try to use it without an implementation.) + out << + indent() << "bool operator < (const " + << tstruct->get_name() << " & ) const;" << endl << endl; } if (read) { out <<