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 93ad7ee..c7df0ee 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -770,6 +770,13 @@
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 <<