borland_cpp.html 11 KB

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