Add an operator< for Thrift structs (in C++).
authorDavid Reiss <dreiss@apache.org>
Wed, 27 Feb 2008 19:38:51 +0000 (19:38 +0000)
committerDavid Reiss <dreiss@apache.org>
Wed, 27 Feb 2008 19:38:51 +0000 (19:38 +0000)
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

compiler/cpp/src/generate/t_cpp_generator.cc

index 93ad7ee..c7df0ee 100644 (file)
@@ -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 <<