THRIFT-1275. cpp: always prefix namespaces with ' ::'
Ensures no accidental namespace clashes.
Patch: Adam Simpkins
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1159729 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 472644e..2b5057d 100755
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -3737,10 +3737,17 @@
* @return Namespaces
*/
string t_cpp_generator::namespace_prefix(string ns) {
+ // Always start with "::", to avoid possible name collisions with
+ // other names in one of the current namespaces.
+ //
+ // We also need a leading space, in case the name is used inside of a
+ // template parameter. "MyTemplate<::foo::Bar>" is not valid C++,
+ // since "<:" is an alternative token for "[".
+ string result = " ::";
+
if (ns.size() == 0) {
- return "";
+ return result;
}
- string result = "";
string::size_type loc;
while ((loc = ns.find(".")) != string::npos) {
result += ns.substr(0, loc);