borland_cpp.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. <h3>[function address] Resolving addresses of overloaded
  125. functions</h3>
  126. Addresses of overloaded functions are not in all contexts properly
  127. resolved (std:13.4 [over.over]); here is a small example:
  128. <pre>
  129. template&lt;class Arg&gt;
  130. void f( void(*g)(Arg) );
  131. void h(int);
  132. void h(double);
  133. template&lt;class T&gt;
  134. void h2(T);
  135. int main()
  136. {
  137. void (*p)(int) = h; // this works (std:13.4-1.1)
  138. void (*p2)(unsigned char) = h2; // this works as well (std:13.4-1.1)
  139. f&lt;int&gt;(h2); // this also works (std:13.4-1.3)
  140. // "Cannot generate template specialization from h(int)",
  141. // "Could not find a match for f&lt;Arg&gt;(void (*)(int))"
  142. f&lt;double&gt;(h); // should work (std:13.4-1.3)
  143. f( (void(*)(double))h); // C-style cast works (std:13.4-1.6 with 5.4)
  144. // "Overloaded 'h' ambiguous in this context"
  145. f(static_cast&lt;void(*)(double)&gt;(h)); // should work (std:13.4-1.6 with 5.2.9)
  146. }
  147. </pre>
  148. <strong>Workaround:</strong> Always use C-style casts when determining
  149. addresses of (potentially) overloaded functions.
  150. <h3>[string conversion] Converting <code>const char *</code> to
  151. <code>std::string</code></h3>
  152. Implicitly converting <code>const char *</code> parameters to
  153. <code>std::string</code> arguments fails if template functions are
  154. explicitly instantiated (it works in the usual cases, though):
  155. <pre>
  156. #include &lt;string&gt;
  157. template&lt;class T&gt;
  158. void f(const std::string &amp; s)
  159. {}
  160. int main()
  161. {
  162. f&lt;double&gt;("hello"); // "Could not find a match for f&lt;T&gt;(char *)"
  163. }
  164. </pre>
  165. <strong>Workaround:</strong> Avoid explicit template function
  166. instantiations (they have significant problems with Microsoft Visual
  167. C++) and pass default-constructed unused dummy arguments with the
  168. appropriate type. Alternatively, if you wish to keep to the explicit
  169. instantiation, you could use an explicit conversion to
  170. <code>std::string</code> or declare the template function as taking a
  171. <code>const char *</code> parameter.
  172. <h3>[template value defaults] Dependent default arguments for template
  173. value parameters</h3>
  174. Template value parameters which default to an expression dependent on
  175. previous template parameters don't work:
  176. <pre>
  177. template&lt;class T&gt;
  178. struct A
  179. {
  180. static const bool value = true;
  181. };
  182. // "Templates must be classes or functions", "Declaration syntax error"
  183. template&lt;class T, bool v = A&lt;T&gt;::value&gt;
  184. struct B {};
  185. int main()
  186. {
  187. B&lt;int&gt; x;
  188. }
  189. </pre>
  190. <strong>Workaround:</strong> If the relevant non-type template
  191. parameter is an implementation detail, use inheritance and a fully
  192. qualified identifier (for example, ::N::A&lt;T&gt;::value).
  193. <h3>[function partial ordering] Partial ordering of function
  194. templates</h3>
  195. Partial ordering of function templates, as described in std:14.5.5.2
  196. [temp.func.order], does not work:
  197. <pre>
  198. #include &lt;iostream&gt;
  199. template&lt;class T&gt; struct A {};
  200. template&lt;class T1&gt;
  201. void f(const A&lt;T1&gt; &)
  202. {
  203. std::cout << "f(const A&lt;T1&gt;&)\n";
  204. }
  205. template&lt;class T&gt;
  206. void f(T)
  207. {
  208. std::cout << "f(T)\n";
  209. }
  210. int main()
  211. {
  212. A&lt;double&gt; a;
  213. f(a); // output: f(T) (wrong)
  214. f(1); // output: f(T) (correct)
  215. }
  216. </pre>
  217. <strong>Workaround:</strong> Declare all such functions uniformly as
  218. either taking a value or a reference parameter.
  219. <h3>[instantiate memfun ptr] Instantiation with member function pointer</h3>
  220. When directly instantiating a template with some member function
  221. pointer, which is itself dependent on some template parameter, the
  222. compiler cannot cope:
  223. <pre>
  224. template&lt;class U&gt; class C { };
  225. template&lt;class T&gt;
  226. class A
  227. {
  228. static const int v = C&lt;void (T::*)()&gt;::value;
  229. };
  230. </pre>
  231. <strong>Workaround:</strong> Use an intermediate <code>typedef</code>:
  232. <pre>
  233. template&lt;class U&gt; class C { };
  234. template&lt;class T&gt;
  235. class A
  236. {
  237. typedef void (T::*my_type)();
  238. static const int v = C&lt;my_type&gt;::value;
  239. };
  240. </pre>
  241. (Extracted from e-mail exchange of David Abrahams, Fernando Cacciola,
  242. and Peter Dimov; not actually tested.)
  243. <h2>Library</h2>
  244. <h3>[cmath.abs] Function <code>double std::abs(double)</code>
  245. missing</h3>
  246. The function <code>double std::abs(double)</code> should be defined
  247. (std:26.5-5 [lib.c.math]), but it is not:
  248. <pre>
  249. #include &lt;cmath&gt;
  250. int main()
  251. {
  252. double (*p)(double) = std::abs; // error
  253. }
  254. </pre>
  255. Note that <code>int std::abs(int)</code> will be used without warning
  256. if you write <code>std::abs(5.1)</code>.
  257. <p>
  258. Similar remarks apply to seemingly all of the other standard math
  259. functions, where Borland C++ fails to provide <code>float</code> and
  260. <code>long double</code> overloads.
  261. <p>
  262. <strong>Workaround:</strong> Use <code>std::fabs</code> instead if
  263. type genericity is not required.
  264. <h2>Appendix: Additional issues with Borland C++ version 5.5</h2>
  265. These issues are documented mainly for historic reasons. If you are
  266. still using Borland C++ version 5.5, you are strongly encouraged to
  267. obtain an upgrade to version 5.5.1, which fixes the issues described
  268. in this section.
  269. <h3>[inline friend] Inline friend functions in template classes</h3>
  270. If a friend function of some class has not been declared before the
  271. friend function declaration, the function is declared at the namespace
  272. scope surrounding the class definition. Together with class templates
  273. and inline definitions of friend functions, the code in the following
  274. fragment should declare (and define) a non-template function "bool
  275. N::f(int,int)", which is a friend of class N::A&lt;int&gt;. However,
  276. Borland C++ v5.5 expects the function f to be declared beforehand:
  277. <pre>
  278. namespace N {
  279. template&lt;class T&gt;
  280. class A
  281. {
  282. // "f is not a member of 'N' in function main()"
  283. friend bool f(T x, T y) { return x < y; }
  284. };
  285. }
  286. int main()
  287. {
  288. N::A&lt;int&gt; a;
  289. }
  290. </pre>
  291. This technique is extensively used in boost/operators.hpp. Giving in
  292. to the wish of the compiler doesn't work in this case, because then
  293. the "instantiate one template, get lots of helper functions at
  294. namespace scope" approach doesn't work anymore. Defining
  295. BOOST_NO_OPERATORS_IN_NAMESPACE (a define
  296. BOOST_NO_INLINE_FRIENDS_IN_CLASS_TEMPLATES would match this case
  297. better) works around this problem and leads to another one, see
  298. [using-template].
  299. <p>
  300. <hr>
  301. 2000-09-30 <a href="../people/jens_maurer.htm">Jens Maurer</a>
  302. </body>
  303. </html>
粤ICP备19079148号