borland_cpp.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>Portability Hints: Borland C++ 5.5.1</title>
  6. </head>
  7. <body bgcolor="#FFFFFF" text="#000000">
  8. <table border="1" bgcolor="#007F7F" cellpadding="2">
  9. <tr>
  10. <td bgcolor="#FFFFFF"><img src="../boost.png" alt="boost.png (6897 bytes)" width="277" height="86"></td>
  11. <td><a href="../index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Home</big></font></a></td>
  12. <td><a href="../libs/libraries.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Libraries</big></font></a></td>
  13. <td><a href="../people/people.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>People</big></font></a></td>
  14. <td><a href="faq.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>FAQ</big></font></a></td>
  15. <td><a href="index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>More</big></font></a></td>
  16. </tr>
  17. </table>
  18. <p>
  19. <h1>Portability Hints: Borland C++ 5.5.1</h1>
  20. It is a general aim for boost libraries to be
  21. <a href="lib_guide.htm#Portability">portable</a>. The primary means
  22. for achieving this goal is to adhere to ISO Standard C++. However,
  23. ISO C++ is a broad and complex standard and most compilers are
  24. not fully conformant to ISO C++ yet. In order to achieve portability
  25. in the light of this restriction, it seems advisable to get acquainted
  26. with those language features that some compilers do not fully
  27. implement yet.
  28. <p>
  29. This page gives portability hints on some language features of the
  30. Borland C++ version 5.5.1 compiler. Furthermore, the appendix
  31. presents additional problems with Borland C++ version 5.5. Borland
  32. C++ 5.5.1 is a freely available command-line compiler for Win32
  33. available at
  34. <a href="http://www.borland.com/">http://www.borland.com/</a>.
  35. <p>
  36. Each entry in the following list describes a particular issue,
  37. complete with sample source code to demonstrate the effect.
  38. Most sample code herein has been verified to compile with gcc 2.95.2
  39. and Comeau C++ 4.2.44.
  40. <h2>Preprocessor symbol</h2>
  41. The preprocessor symbol <code>__BORLANDC__</code> is defined for all
  42. Borland C++ compilers. Its value is the version number of the
  43. compiler interpreted as a hexadecimal number. The following table
  44. lists some known values.
  45. <p>
  46. <table border="1">
  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. 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
  78. where there are none. The following code fragment illustrates this:
  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. 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++:
  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. Template function type deduction should omit top-level constness.
  107. However, this code fragment instantiates "f&lt;const int&gt;(int)":
  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. The boost/rational.hpp header exhibits this problem in connection with
  125. the gcd() function.
  126. <h3>[function address] Resolving addresses of overloaded
  127. functions</h3>
  128. Addresses of overloaded functions are not in all contexts properly
  129. resolved (std:13.4 [over.over]); here is a small example:
  130. <pre>
  131. template&lt;class Arg&gt;
  132. void f( void(*g)(Arg) );
  133. void h(int);
  134. void h(double);
  135. template&lt;class T&gt;
  136. void h2(T);
  137. int main()
  138. {
  139. void (*p)(int) = h; // this works (std:13.4-1.1)
  140. void (*p2)(unsigned char) = h2; // this works as well (std:13.4-1.1)
  141. f&lt;int&gt;(h2); // this also works (std:13.4-1.3)
  142. // "Cannot generate template specialization from h(int)",
  143. // "Could not find a match for f&lt;Arg&gt;(void (*)(int))"
  144. f&lt;double&gt;(h); // should work (std:13.4-1.3)
  145. f( (void(*)(double))h); // C-style cast works (std:13.4-1.6 with 5.4)
  146. // "Overloaded 'h' ambiguous in this context"
  147. f(static_cast&lt;void(*)(double)&gt;(h)); // should work (std:13.4-1.6 with 5.2.9)
  148. }
  149. </pre>
  150. <strong>Workaround:</strong> Always use C-style casts when determining
  151. addresses of (potentially) overloaded functions.
  152. <h3>[string conversion] Converting <code>const char *</code> to
  153. <code>std::string</code></h3>
  154. Implicitly converting <code>const char *</code> parameters to
  155. <code>std::string</code> arguments fails if template functions are
  156. explicitly instantiated (it works in the usual cases, though):
  157. <pre>
  158. #include &lt;string&gt;
  159. template&lt;class T&gt;
  160. void f(const std::string &amp; s)
  161. {}
  162. int main()
  163. {
  164. f&lt;double&gt;("hello"); // "Could not find a match for f&lt;T&gt;(char *)"
  165. }
  166. </pre>
  167. <strong>Workaround:</strong> Avoid explicit template function
  168. instantiations (they have significant problems with Microsoft Visual
  169. C++) and pass default-constructed unused dummy arguments with the
  170. appropriate type. Alternatively, if you wish to keep to the explicit
  171. instantiation, you could use an explicit conversion to
  172. <code>std::string</code> or declare the template function as taking a
  173. <code>const char *</code> parameter.
  174. <h3>[template value defaults] Dependent default arguments for template
  175. value parameters</h3>
  176. Template value parameters which default to an expression dependent on
  177. previous template parameters don't work:
  178. <pre>
  179. template&lt;class T&gt;
  180. struct A
  181. {
  182. static const bool value = true;
  183. };
  184. // "Templates must be classes or functions", "Declaration syntax error"
  185. template&lt;class T, bool v = A&lt;T&gt;::value&gt;
  186. struct B {};
  187. int main()
  188. {
  189. B&lt;int&gt; x;
  190. }
  191. </pre>
  192. <strong>Workaround:</strong> If the relevant non-type template
  193. parameter is an implementation detail, use inheritance and a fully
  194. qualified identifier (for example, ::N::A&lt;T&gt;::value).
  195. <h3>[function partial ordering] Partial ordering of function
  196. templates</h3>
  197. Partial ordering of function templates, as described in std:14.5.5.2
  198. [temp.func.order], does not work:
  199. <pre>
  200. #include &lt;iostream&gt;
  201. template&lt;class T&gt; struct A {};
  202. template&lt;class T1&gt;
  203. void f(const A&lt;T1&gt; &)
  204. {
  205. std::cout << "f(const A&lt;T1&gt;&)\n";
  206. }
  207. template&lt;class T&gt;
  208. void f(T)
  209. {
  210. std::cout << "f(T)\n";
  211. }
  212. int main()
  213. {
  214. A&lt;double&gt; a;
  215. f(a); // output: f(T) (wrong)
  216. f(1); // output: f(T) (correct)
  217. }
  218. </pre>
  219. <strong>Workaround:</strong> Declare all such functions uniformly as
  220. either taking a value or a reference parameter.
  221. <h3>[instantiate memfun ptr] Instantiation with member function pointer</h3>
  222. When directly instantiating a template with some member function
  223. pointer, which is itself dependent on some template parameter, the
  224. compiler cannot cope:
  225. <pre>
  226. template&lt;class U&gt; class C { };
  227. template&lt;class T&gt;
  228. class A
  229. {
  230. static const int v = C&lt;void (T::*)()&gt;::value;
  231. };
  232. </pre>
  233. <strong>Workaround:</strong> Use an intermediate <code>typedef</code>:
  234. <pre>
  235. template&lt;class U&gt; class C { };
  236. template&lt;class T&gt;
  237. class A
  238. {
  239. typedef void (T::*my_type)();
  240. static const int v = C&lt;my_type&gt;::value;
  241. };
  242. </pre>
  243. (Extracted from e-mail exchange of David Abrahams, Fernando Cacciola,
  244. and Peter Dimov; not actually tested.)
  245. <h2>Library</h2>
  246. <h3>[cmath.abs] Function <code>double std::abs(double)</code>
  247. missing</h3>
  248. The function <code>double std::abs(double)</code> should be defined
  249. (std:26.5-5 [lib.c.math]), but it is not:
  250. <pre>
  251. #include &lt;cmath&gt;
  252. int main()
  253. {
  254. double (*p)(double) = std::abs; // error
  255. }
  256. </pre>
  257. Note that <code>int std::abs(int)</code> will be used without warning
  258. if you write <code>std::abs(5.1)</code>.
  259. <p>
  260. Similar remarks apply to seemingly all of the other standard math
  261. functions, where Borland C++ fails to provide <code>float</code> and
  262. <code>long double</code> overloads.
  263. <p>
  264. <strong>Workaround:</strong> Use <code>std::fabs</code> instead if
  265. type genericity is not required.
  266. <h2>Appendix: Additional issues with Borland C++ version 5.5</h2>
  267. These issues are documented mainly for historic reasons. If you are
  268. still using Borland C++ version 5.5, you are strongly encouraged to
  269. obtain an upgrade to version 5.5.1, which fixes the issues described
  270. in this section.
  271. <h3>[inline friend] Inline friend functions in template classes</h3>
  272. If a friend function of some class has not been declared before the
  273. friend function declaration, the function is declared at the namespace
  274. scope surrounding the class definition. Together with class templates
  275. and inline definitions of friend functions, the code in the following
  276. fragment should declare (and define) a non-template function "bool
  277. N::f(int,int)", which is a friend of class N::A&lt;int&gt;. However,
  278. Borland C++ v5.5 expects the function f to be declared beforehand:
  279. <pre>
  280. namespace N {
  281. template&lt;class T&gt;
  282. class A
  283. {
  284. // "f is not a member of 'N' in function main()"
  285. friend bool f(T x, T y) { return x < y; }
  286. };
  287. }
  288. int main()
  289. {
  290. N::A&lt;int&gt; a;
  291. }
  292. </pre>
  293. This technique is extensively used in boost/operators.hpp. Giving in
  294. to the wish of the compiler doesn't work in this case, because then
  295. the "instantiate one template, get lots of helper functions at
  296. namespace scope" approach doesn't work anymore. Defining
  297. BOOST_NO_OPERATORS_IN_NAMESPACE (a define
  298. BOOST_NO_INLINE_FRIENDS_IN_CLASS_TEMPLATES would match this case
  299. better) works around this problem and leads to another one, see
  300. [using-template].
  301. <p>
  302. <hr>
  303. 2000-09-30 <a href="../people/jens_maurer.htm">Jens Maurer</a>
  304. </body>
  305. </html>
粤ICP备19079148号