microsoft_vcpp.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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: Microsoft Visual C++ 6.0 SP4</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: Microsoft Visual C++ 6.0 SP4</h1>
  26. <p>Similar to the <a href="borland_cpp.html">portability hints for Borland
  27. C++</a>, this page provides hints on some language features of the
  28. Microsoft Visual C++ version 6.0 service pack 4 compiler. A list of
  29. acknowledged deficiencies can be found at the <a href=
  30. "http://support.microsoft.com/support/kb/articles/q243/4/51.asp">Microsoft
  31. support site</a>.</p>
  32. <p>Each entry in the following list describes a particular issue, complete
  33. with sample source code to demonstrate the effect. Most sample code herein
  34. has been verified to compile with gcc 2.95.2 and Comeau C++ 4.2.44.</p>
  35. <h2>Preprocessor symbol</h2>
  36. <p>The preprocessor symbol <code>_MSC_VER</code> is defined for all
  37. Microsoft C++ compilers. Its value is the internal version number of the
  38. compiler interpreted as a decimal number. Since a few other compilers also
  39. define this symbol, boost provides the symbol <code>BOOST_MSVC</code>,
  40. which is defined in <a href="../boost/config.hpp">boost/config.hpp</a> to
  41. the value of _MSC_VER if and only if the compiler is really Microsoft
  42. Visual C++. The following table lists some known values.</p>
  43. <table border="1" summary="">
  44. <tr>
  45. <th>Compiler</th>
  46. <th><code>BOOST_MSVC</code> value</th>
  47. </tr>
  48. <tr>
  49. <td>Microsoft Visual C++ 6.0 (up to SP6)</td>
  50. <td>1200</td>
  51. </tr>
  52. <tr>
  53. <td>Microsoft embedded Visual C++ 4.0</td>
  54. <td>1200-1202 (cross compilers)</td>
  55. </tr>
  56. </table>
  57. <h2>Core Language</h2>
  58. <h3>[chained using] Chaining <code>using</code>-declarations</h3>
  59. <p>Chaining <code>using</code>-declarations does not work.</p>
  60. <pre>
  61. void f();
  62. namespace N {
  63. using ::f;
  64. }
  65. void g()
  66. {
  67. using N::f; // C2873: 'f': the symbol cannot be used in a using-declaration
  68. }
  69. </pre>
  70. <h3>[explicit-instantiation] Explicit function template instantiation</h3>
  71. <p>Trying to explicitly instantiate a function template leads to the wrong
  72. function being called silently.</p>
  73. <pre>
  74. #include &lt;stdio.h&gt;
  75. template&lt;class T&gt;
  76. void f()
  77. {
  78. printf("%d\n", sizeof(T));
  79. }
  80. int main()
  81. {
  82. f&lt;double&gt;(); // output: "1"
  83. f&lt;char&gt;(); // output: "1"
  84. return 0;
  85. }
  86. </pre>
  87. <h3>[for-scoping] Scopes of definitions in for-loops</h3>
  88. <p>The scope of variable definitions in <code>for</code> loops should be
  89. local to the loop's body, but it is instead local to the enclosing
  90. block.</p>
  91. <pre>
  92. int main()
  93. {
  94. for(int i = 0; i &lt; 5; ++i)
  95. ;
  96. for(int i = 0; i &lt; 5; ++i) // C2374: 'i': Redefinition; multiple initialization
  97. ;
  98. return 0;
  99. }
  100. </pre>
  101. <p><strong>Workaround:</strong> Enclose the offending <code>for</code>
  102. loops in another pair of curly braces.</p>
  103. <p>Another possible workaround (brought to my attention by Vesa Karvonen)
  104. is this:</p>
  105. <pre>
  106. #ifndef for
  107. #define for if (0) {} else for
  108. #endif
  109. </pre>
  110. <p>Note that platform-specific inline functions in included headers might
  111. depend on the old-style <code>for</code> scoping.</p>
  112. <h3>[inclass-member-init] In-class member initialization</h3>
  113. <p>In-class member initialization, required to implement a
  114. Standard-conforming <code>std::numeric_limits</code> template, does not
  115. work.</p>
  116. <pre>
  117. struct A
  118. {
  119. static const int i = 5; // "invalid syntax for pure virtual method"
  120. };
  121. </pre>
  122. <p><strong>Workaround:</strong> Either use an enum (which has incorrect
  123. type, but can be used in compile-time constant expressions), or define the
  124. value out-of-line (which allows for the correct type, but prohibits using
  125. the constant in compile-time constant expressions). See <a href=
  126. "int_const_guidelines.htm">Coding Guidelines for Integral Constant
  127. Expressions</a> for guidelines how to define member constants portably in
  128. boost libraries.</p>
  129. <h3>[koenig-lookup] Argument-dependent lookup</h3>
  130. <p>Argument-dependent lookup, also called Koenig lookup, works for
  131. overloaded operators, but not for ordinary functions. No additional
  132. namespaces induced from the argument types seem to be considered.</p>
  133. <pre>
  134. namespace N {
  135. struct A {};
  136. void f(A);
  137. }
  138. void g()
  139. {
  140. N::A a;
  141. f(a); // 'f': undeclared identifier
  142. }
  143. </pre>
  144. <h3>[template-friend] Templates as friends</h3>
  145. <p>A Template cannot be declared a friend of a class.</p>
  146. <pre>
  147. template&lt;class T&gt;
  148. struct A {};
  149. struct B
  150. {
  151. template&lt;class T&gt;
  152. friend struct A; // "syntax error"
  153. };
  154. </pre>
  155. <h3>[member-template-outofline] Out-of-line definitions of member
  156. templates</h3>
  157. <p>Defining member templates outside their enclosing class does not
  158. work.</p>
  159. <pre>
  160. template&lt;class T&gt;
  161. struct A
  162. {
  163. template&lt;class U&gt;
  164. void f();
  165. };
  166. template&lt;class T&gt;
  167. template&lt;class U&gt; // "syntax error"
  168. void A&lt;T&gt;::f() // "T: undeclared identifier"
  169. {
  170. }
  171. </pre>
  172. <p><strong>Workaround:</strong> Define member templates in-line within
  173. their enclosing class.</p>
  174. <h3>[partial-spec] Partial specialization</h3>
  175. <p>Partial specialization of class templates does not work.</p>
  176. <pre>
  177. template&lt;class T&gt;
  178. struct A {};
  179. template&lt;class T&gt;
  180. struct B {};
  181. template&lt;class T&gt;
  182. struct A&lt;B&lt;T&gt; &gt; {}; // template class was already defined as a non-template
  183. </pre>
  184. <p><strong>Workaround:</strong> In some situations where interface does not
  185. matter, class member templates can simulate partial specialization.</p>
  186. <h3>[template-value] Dependent template value parameters</h3>
  187. <p>Template value parameters whose type depends on a previous template
  188. parameter provoke an internal compiler error if the correct syntax (with
  189. "typename") is used.</p>
  190. <pre>
  191. template&lt;class T, typename T::result_type&gt; // C1001: INTERNAL COMPILER ERROR: msc1.cpp, line 1794
  192. struct B {};
  193. // (omit "typename" and it compiles)
  194. </pre>
  195. <p><strong>Workaround:</strong> Leave off the "typename" keyword. That
  196. makes the program non-conforming, though.</p>
  197. <h3>[wchar_t] <code>wchar_t</code> is not built-in</h3>
  198. <p>The type <code>wchar_t</code> is not a built-in type.</p>
  199. <pre>
  200. wchar_t x; // "missing storage class or type identifier"
  201. </pre>
  202. <p><strong>Workaround:</strong> When using Microsoft Visual C++, the header
  203. <a href="../boost/config.hpp">boost/config.hpp</a> includes
  204. <code>&lt;cstddef&gt;</code>, which defines <code>wchar_t</code> as a
  205. typedef for <code>unsigned short</code>. Note that this means that the
  206. compiler does not regard <code>wchar_t</code> and <code>unsigned
  207. short</code> as distinct types, as is required by the standard, and so
  208. ambiguities may emanate when overloading on <code>wchar_t</code>. The macro
  209. <code>BOOST_NO_INTRINSIC_WCHAR_T</code> is defined in this situation.</p>
  210. <h3>[delete-const-pointer] Deleting <code>const X *</code> does not
  211. work</h3>
  212. <p>Trying to delete a pointer to a cv-qualified type gives an error:</p>
  213. <pre>
  214. void f()
  215. {
  216. const int *p = new int(5);
  217. delete p; // C2664: cannot convert from "const int *" to "void *"
  218. }
  219. </pre>
  220. <p><strong>Workaround:</strong> Define the function</p>
  221. <pre>
  222. inline void operator delete(const void *p) throw()
  223. { operator delete(const_cast&lt;void*&gt;(p)); }
  224. </pre>
  225. <p>and similar functions for the other cv-qualifier combinations, for
  226. operator delete[], and for the <code>std::nothrow</code> variants.</p>
  227. <h2>Standard Library</h2>
  228. <h3>[clib-namespace] C library names in global namespace instead of
  229. std</h3>
  230. <p>Library names from the &lt;c...&gt; headers are in the global namespace
  231. instead of namespace std.</p>
  232. <p><b>Workaround:</b>&nbsp; The header <a href=
  233. "../libs/config/config.htm">boost/config.hpp</a> will define
  234. BOOST_NO_STDC_NAMESPACE. It can be used as follows:</p>
  235. <pre>
  236. # ifdef BOOST_NO_STDC_NAMESPACE
  237. namespace std { using ::abs; using ::fabs; }
  238. # endif
  239. </pre>
  240. <p>Because std::size_t and std::ptrdiff_t are so commonly used, the
  241. workaround for these is already provided in boost/config.hpp.</p>
  242. <hr>
  243. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  244. "http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
  245. height="31" width="88"></a></p>
  246. <p>Revised
  247. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->04 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38514" --></p>
  248. <p><i>Copyright &copy; 2001-2002 <a href="../people/jens_maurer.htm">Jens
  249. Maurer</a></i></p>
  250. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  251. accompanying file <a href="../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
  252. at <a href=
  253. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  254. </body>
  255. </html>
粤ICP备19079148号