code_junkie

How to avoid a common bug in a large codebase?

Nov 13th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. int age = 27;
  2.  
  3. std::wstring str = std::wstring(L"User's age is: ");
  4. str += age;
  5.  
  6. std::string str2 = std::string("User's age is: ");
  7. str2 += age;
  8.  
  9. namespace myapp
  10. {
  11. typedef std::string String;
  12. typedef std::wstring UTFString;
  13. }
  14.  
  15. namespace myapp
  16. {
  17. /** std::basic_string with limited and controlled interface.
  18. */
  19. template< class _Elem, class _Traits, class _Ax >
  20. class limited_string
  21. {
  22. public:
  23. typedef std::basic_string< _Elem , _Traits, _Ax > _String; // this is for easier writing
  24. typedef limited_string< _Elem, _Traits, _Ax > _MyType; // this is for easier writing
  25.  
  26. private:
  27. _String m_string; // here the real std::basic_string object that will do all the real work!
  28.  
  29. public:
  30.  
  31. // constructor proxies... (note that those ones are not complete, it should be exactly the same as the original std::basic_string
  32. // see some STL docs to get the real interface to rewrite)
  33. limited_string() : m_string {}
  34. limited_string( const _MyType& l_string ) : m_string( l_string.m_string ) {}
  35. limited_string( const _Elem* raw_string ) : m_string( raw_string ) {}
  36. //... etc...
  37.  
  38. // operator proxies...
  39. _MyType& operator= ( const _MyType& l_string )
  40. {
  41. m_string = l_string.m_string;
  42. }
  43. // etc...
  44. // but we don't want the operator += with int values so we DON'T WRITE IT!
  45.  
  46. // other function proxies...
  47. size_t size() const { return m_string.size(); } // simply forward the call to the real string!
  48. // etc...you know what i mean...
  49.  
  50. // to work automatically with other STL algorithm and functions we add automatic conversion functions:
  51. operator const _Elem*() const { return m_string.c_str(); }
  52.  
  53. // etc..
  54.  
  55.  
  56. };
  57. }
  58.  
  59. // instead of those lines...
  60. //typedef std::string String;
  61. //typedef std::wstring UTFString;
  62.  
  63. // use those ones
  64. typedef limited_string< char, std::char_traits<char>, std::allocator<char> > String; // like std::string typedef
  65. typedef limited_string< wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > UTFString; // like std::wstring typedef
  66.  
  67. error C2676: binary '+=' : 'myapp::UTFString' does not define this operator or a conversion to a type acceptable to the predefined operator
  68. error C2676: binary '+=' : 'myapp::String' does not define this operator or a conversion to a type acceptable to the predefined operator
  69.  
  70. #include <string>
  71. #include <iostream>
  72.  
  73. namespace myapp
  74. {
  75.  
  76. /** std::basic_string with limited and controlled interface.
  77. */
  78. template< class _Elem, class _Traits, class _Ax >
  79. class limited_string
  80. {
  81. public:
  82. typedef std::basic_string< _Elem , _Traits, _Ax > _String; // this is for easier writing
  83. typedef limited_string< _Elem, _Traits, _Ax > _MyType; // this is for easier writing
  84.  
  85. private:
  86. _String m_string; // here the real std::basic_string object that will do all the real work!
  87.  
  88. public:
  89.  
  90. // constructor proxies... (note that those ones are not complete, it should be exactly the same as the original std::basic_string
  91. // see some STL docs to get the real interface to rewrite)
  92. limited_string() : m_string {}
  93. limited_string( const _MyType& l_string ) : m_string( l_string.m_string ) {}
  94. limited_string( const _Elem* raw_string ) : m_string( raw_string ) {}
  95. //... etc...
  96.  
  97. // operator proxies...
  98. _MyType& operator= ( const _MyType& l_string )
  99. {
  100. m_string = l_string.m_string;
  101. }
  102. // etc...
  103. // but we don't want the operator += with int values so we DON'T WRITE IT!
  104.  
  105. // other function proxies...
  106. size_t size() const { return m_string.size(); } // simply forward the call to the real string!
  107. // etc...you know what i mean...
  108.  
  109. // to work automatically with other STL algorithm and functions we add automatic conversion functions:
  110. operator const _Elem*() const { return m_string.c_str(); }
  111.  
  112. // etc..
  113.  
  114.  
  115. };
  116.  
  117. // instead of those lines...
  118. //typedef std::string String;
  119. //typedef std::wstring UTFString;
  120.  
  121. // use those ones
  122. typedef limited_string< char, std::char_traits<char>, std::allocator<char> > String; // like std::string typedef
  123. typedef limited_string< wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > UTFString; // like std::wstring typedef
  124. }
  125.  
  126. int main()
  127. {
  128. using namespace myapp;
  129.  
  130. int age = 27;
  131.  
  132. UTFString str = UTFString(L"User's age is: ");
  133. str += age; // compilation error!
  134. std::wcout << str << std::endl;
  135.  
  136. String str2 = String("User's age is: ");
  137. str2 += age; // compilation error!
  138. std::cout << str2 << std::endl;
  139.  
  140. std::cin.ignore();
  141.  
  142. return 0;
  143. }
  144.  
  145. #!/bin/tcsh
  146. # Pass the file to test as the first argument.
  147.  
  148. echo "#include <string>
  149. void operator+=(std::string const& , int const&);
  150. void operator+=(std::string const& , int);"
  151. | cat - $1
  152. | g++ -c -x c++ - >& /dev/null
  153.  
  154.  
  155. echo $status
  156.  
  157. MyString str;
  158. str += 27;
Add Comment
Please, Sign In to add comment