borland_cpp.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <title>Portability Hints: Borland C++ 5.5.1</title>
  5. </head>
  6. <body bgcolor="#FFFFFF" text="#000000">
  7. <table border="1" bgcolor="#007F7F" cellpadding="2">
  8. <tr>
  9. <td bgcolor="#FFFFFF"><img src="../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td>
  10. <td><a href="../index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Home</big></font></a></td>
  11. <td><a href="../libs/libraries.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Libraries</big></font></a></td>
  12. <td><a href="../people/people.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>People</big></font></a></td>
  13. <td><a href="faq.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>FAQ</big></font></a></td>
  14. <td><a href="index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>More</big></font></a></td>
  15. </tr>
  16. </table>
  17. <p>
  18. <h1>Portability Hints: Borland C++ 5.5.1</h1>
  19. It is a general aim for boost libraries to be
  20. <a href="lib_guide.htm#Portability">portable</a>. The primary means
  21. for achieving this goal is to adhere to ISO Standard C++. However,
  22. ISO C++ is a broad and complex standard and most compilers are
  23. not fully conformant to ISO C++ yet. In order to achieve portability
  24. in the light of this restriction, it seems advisable to get acquainted
  25. with those language features that some compilers do not fully
  26. implement yet.
  27. <p>
  28. This page gives portability hints on some language features of the
  29. Borland C++ version 5.5.1 compiler. Furthermore, the appendix
  30. presents additional problems with Borland C++ version 5.5. Borland
  31. C++ 5.5.1 is a freely available command-line compiler for Win32
  32. available at
  33. <a href="http://www.borland.com/">http://www.borland.com/</a>.
  34. <p>
  35. Each entry in the following list describes a particular issue,
  36. complete with sample source code to demonstrate the effect.
  37. Most sample code herein has been verified to compile with gcc 2.95.2
  38. and Comeau C++ 4.2.44.
  39. <h2>Preprocessor symbol</h2>
  40. The preprocessor symbol <code>__BORLANDC__</code> is defined for all
  41. Borland C++ compilers. Its value is the version number of the
  42. compiler interpreted as a hexadecimal number. The following table
  43. lists some known values.
  44. <p>
  45. <table border="1">
  46. <tr>
  47. <th>Compiler</th>
  48. <th><code>__BORLANDC__</code> value</th>
  49. </tr>
  50. <tr>
  51. <td>Borland C++ Builder 4</td>
  52. <td>0x0540</td>
  53. </tr>
  54. <tr>
  55. <td>Borland C++ Builder 5</td>
  56. <td>0x0550</td>
  57. </tr>
  58. <tr>
  59. <td>Borland C++ 5.5</td>
  60. <td>0x0550</td>
  61. </tr>
  62. <tr>
  63. <td>Borland C++ 5.5.1</td>
  64. <td>0x0551</td>
  65. </tr>
  66. </table>
  67. <h2>Core Language</h2>
  68. <h3>[using-directive] Mixing <code>using</code>-declarations and
  69. <code>using</code>-directives</h3>
  70. Mixing <code>using</code>-directives (which refer to whole namespaces)
  71. and namespace-level <code>using</code>-declarations (which refer to
  72. individual identifiers within foreign namespaces) causes ambiguities
  73. where there are none. The following code fragment illustrates this:
  74. <pre>
  75. namespace N {
  76. int x();
  77. }
  78. using N::x;
  79. using namespace N;
  80. int main()
  81. {
  82. &x; // Ambiguous overload
  83. }
  84. </pre>
  85. <h3>[using template] <code>using</code>-declarations for class
  86. templates</h3>
  87. Identifiers for class templates can be used as arguments to
  88. <code>using</code>-declarations as any other identifier. However, the
  89. following code fails to compile with Borland C++:
  90. <pre>
  91. template&lt;class T&gt;
  92. class X { };
  93. namespace N
  94. {
  95. // "cannot use template 'X<T>' without specifying specialization parameters"
  96. using ::X;
  97. };
  98. </pre>
  99. <h3>[template const arg] Deduction of constant arguments to function
  100. templates</h3>
  101. Template function type deduction should omit top-level constness.
  102. However, this code fragment instantiates "f&lt;const int&gt;(int)":
  103. <pre>
  104. template&lt;class T&gt;
  105. void f(T x)
  106. {
  107. x = 1; // works
  108. (void) &x;
  109. T y = 17;
  110. y = 20; // "Cannot modify a const object in function f<const int>(int)"
  111. (void) &y;
  112. }
  113. int main()
  114. {
  115. const int i = 17;
  116. f(i);
  117. }
  118. </pre>
  119. The boost/rational.hpp header exhibits this problem in connection with
  120. the gcd() function.
  121. <h3>[function address] Resolving addresses of overloaded
  122. functions</h3>
  123. Addresses of overloaded functions are not in all contexts properly
  124. resolved (std:13.4 [over.over]); here is a small example:
  125. <pre>
  126. template&lt;class Arg&gt;
  127. void f( void(*g)(Arg) );
  128. void h(int);
  129. void h(double);
  130. template&lt;class T&gt;
  131. void h2(T);
  132. int main()
  133. {
  134. void (*p)(int) = h; // this works (std:13.4-1.1)
  135. void (*p2)(unsigned char) = h2; // this works as well (std:13.4-1.1)
  136. f&lt;int&gt;(h2); // this also works (std:13.4-1.3)
  137. // "Cannot generate template specialization from h(int)",
  138. // "Could not find a match for f&lt;Arg&gt;(void (*)(int))"
  139. f&lt;double&gt;(h); // should work (std:13.4-1.3)
  140. f( (void(*)(double))h); // C-style cast works (std:13.4-1.6 with 5.4)
  141. // "Overloaded 'h' ambiguous in this context"
  142. f(static_cast&lt;void(*)(double)&gt;(h)); // should work (std:13.4-1.6 with 5.2.9)
  143. }
  144. </pre>
  145. <strong>Workaround:</strong> Always use C-style casts when determining
  146. addresses of (potentially) overloaded functions.
  147. <h3>[string conversion] Converting <code>const char *</code> to
  148. <code>std::string</code></h3>
  149. Implicitly converting <code>const char *</code> parameters to
  150. <code>std::string</code> arguments fails if template functions are
  151. explicitly instantiated (it works in the usual cases, though):
  152. <pre>
  153. #include &lt;string&gt;
  154. template&lt;class T&gt;
  155. void f(const std::string &amp; s)
  156. {}
  157. int main()
  158. {
  159. f&lt;double&gt;("hello"); // "Could not find a match for f&lt;T&gt;(char *)"
  160. }
  161. </pre>
  162. <strong>Workaround:</strong> Avoid explicit template function
  163. instantiations (they have significant problems with Microsoft Visual
  164. C++) and pass default-constructed unused dummy arguments with the
  165. appropriate type. Alternatively, if you wish to keep to the explicit
  166. instantiation, you could use an explicit conversion to
  167. <code>std::string</code> or declare the template function as taking a
  168. <code>const char *</code> parameter.
  169. <h3>[template value defaults] Dependent default arguments for template
  170. value parameters</h3>
  171. Template value parameters which default to an expression dependent on
  172. previous template parameters don't work:
  173. <pre>
  174. template&lt;class T&gt;
  175. struct A
  176. {
  177. static const bool value = true;
  178. };
  179. // "Templates must be classes or functions", "Declaration syntax error"
  180. template&lt;class T, bool v = A&lt;T&gt;::value&gt;
  181. struct B {};
  182. int main()
  183. {
  184. B&lt;int&gt; x;
  185. }
  186. </pre>
  187. <strong>Workaround:</strong> If the relevant non-type template
  188. parameter is an implementation detail, use inheritance and a fully
  189. qualified identifier (for example, ::N::A&lt;T&gt;::value).
  190. <h3>[function partial ordering] Partial ordering of function
  191. templates</h3>
  192. Partial ordering of function templates, as described in std:14.5.5.2
  193. [temp.func.order], does not work:
  194. <pre>
  195. #include &lt;iostream&gt;
  196. template&lt;class T&gt; struct A {};
  197. template&lt;class T1&gt;
  198. void f(const A&lt;T1&gt; &)
  199. {
  200. std::cout << "f(const A&lt;T1&gt;&)\n";
  201. }
  202. template&lt;class T&gt;
  203. void f(T)
  204. {
  205. std::cout << "f(T)\n";
  206. }
  207. int main()
  208. {
  209. A&lt;double&gt; a;
  210. f(a); // output: f(T) (wrong)
  211. f(1); // output: f(T) (correct)
  212. }
  213. </pre>
  214. <strong>Workaround:</strong> Declare all such functions uniformly as
  215. either taking a value or a reference parameter.
  216. <h2>Library</h2>
  217. <h3>[cmath.abs] Function <code>double std::abs(double)</code>
  218. missing</h3>
  219. The function <code>double std::abs(double)</code> should be defined
  220. (std:26.5-5 [lib.c.math]), but it is not:
  221. <pre>
  222. #include &lt;cmath&gt;
  223. int main()
  224. {
  225. double (*p)(double) = std::abs; // error
  226. }
  227. </pre>
  228. Note that <code>int std::abs(int)</code> will be used without warning
  229. if you write <code>std::abs(5.1)</code>.
  230. <p>
  231. Similar remarks apply to seemingly all of the other standard math
  232. functions, where Borland C++ fails to provide <code>float</code> and
  233. <code>long double</code> overloads.
  234. <p>
  235. <strong>Workaround:</strong> Use <code>std::fabs</code> instead if
  236. type genericity is not required.
  237. <h2>Appendix: Additional issues with Borland C++ version 5.5</h2>
  238. These issues are documented mainly for historic reasons. If you are
  239. still using Borland C++ version 5.5, you are strongly encouraged to
  240. obtain an upgrade to version 5.5.1, which fixes the issues described
  241. in this section.
  242. <h3>[inline friend] Inline friend functions in template classes</h3>
  243. If a friend function of some class has not been declared before the
  244. friend function declaration, the function is declared at the namespace
  245. scope surrounding the class definition. Together with class templates
  246. and inline definitions of friend functions, the code in the following
  247. fragment should declare (and define) a non-template function "bool
  248. N::f(int,int)", which is a friend of class N::A&lt;int&gt;. However,
  249. Borland C++ v5.5 expects the function f to be declared beforehand:
  250. <pre>
  251. namespace N {
  252. template&lt;class T&gt;
  253. class A
  254. {
  255. // "f is not a member of 'N' in function main()"
  256. friend bool f(T x, T y) { return x < y; }
  257. };
  258. }
  259. int main()
  260. {
  261. N::A&lt;int&gt; a;
  262. }
  263. </pre>
  264. This technique is extensively used in boost/operators.hpp. Giving in
  265. to the wish of the compiler doesn't work in this case, because then
  266. the "instantiate one template, get lots of helper functions at
  267. namespace scope" approach doesn't work anymore. Defining
  268. BOOST_NO_OPERATORS_IN_NAMESPACE (a define
  269. BOOST_NO_INLINE_FRIENDS_IN_CLASS_TEMPLATES would match this case
  270. better) works around this problem and leads to another one, see
  271. [using-template].
  272. <p>
  273. <hr>
  274. 2000-09-30 <a href="../people/jens_maurer.htm">Jens Maurer</a>
  275. </body>
  276. </html>
粤ICP备19079148号