Improve vacpp support.
[boost.git] / boost / more / microsoft_vcpp.html
blob7af0c4e7ceb198f37f26a1b9e8df8612b1a054d6
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <meta http-equiv="Content-Language" content="en-us">
6 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
8 <title>Portability Hints: Microsoft Visual C++ 6.0 SP4</title>
9 </head>
11 <body bgcolor="#FFFFFF" text="#000000">
12 <table border="1" bgcolor="#007F7F" cellpadding="2" summary="">
13 <tr>
14 <td bgcolor="#FFFFFF"><img src="../boost.png" alt=
15 "boost.png (6897 bytes)" width="277" height="86"></td>
17 <td><a href="../index.htm"><font face="Arial,Helvetica" color=
18 "#FFFFFF"><big>Home</big></font></a></td>
20 <td><a href="../libs/libraries.htm"><font face="Arial,Helvetica" color=
21 "#FFFFFF"><big>Libraries</big></font></a></td>
23 <td><a href="../people/people.htm"><font face="Arial,Helvetica" color=
24 "#FFFFFF"><big>People</big></font></a></td>
26 <td><a href="faq.htm"><font face="Arial,Helvetica" color=
27 "#FFFFFF"><big>FAQ</big></font></a></td>
29 <td><a href="index.htm"><font face="Arial,Helvetica" color=
30 "#FFFFFF"><big>More</big></font></a></td>
31 </tr>
32 </table>
34 <h1>Portability Hints: Microsoft Visual C++ 6.0 SP4</h1>
36 <p>Similar to the <a href="borland_cpp.html">portability hints for Borland
37 C++</a>, this page provides hints on some language features of the
38 Microsoft Visual C++ version 6.0 service pack 4 compiler. A list of
39 acknowledged deficiencies can be found at the <a href=
40 "http://support.microsoft.com/support/kb/articles/q243/4/51.asp">Microsoft
41 support site</a>.</p>
43 <p>Each entry in the following list describes a particular issue, complete
44 with sample source code to demonstrate the effect. Most sample code herein
45 has been verified to compile with gcc 2.95.2 and Comeau C++ 4.2.44.</p>
47 <h2>Preprocessor symbol</h2>
49 <p>The preprocessor symbol <code>_MSC_VER</code> is defined for all
50 Microsoft C++ compilers. Its value is the internal version number of the
51 compiler interpreted as a decimal number. Since a few other compilers also
52 define this symbol, boost provides the symbol <code>BOOST_MSVC</code>,
53 which is defined in <a href="../boost/config.hpp">boost/config.hpp</a> to
54 the value of _MSC_VER if and only if the compiler is really Microsoft
55 Visual C++. The following table lists some known values.</p>
57 <table border="1" summary="">
58 <tr>
59 <th>Compiler</th>
61 <th><code>BOOST_MSVC</code> value</th>
62 </tr>
64 <tr>
65 <td>Microsoft Visual C++ 6.0 (up to SP6)</td>
67 <td>1200</td>
68 </tr>
70 <tr>
71 <td>Microsoft embedded Visual C++ 4.0</td>
73 <td>1200-1202 (cross compilers)</td>
74 </tr>
75 </table>
77 <h2>Core Language</h2>
79 <h3>[chained using] Chaining <code>using</code>-declarations</h3>
81 <p>Chaining <code>using</code>-declarations does not work.</p>
82 <pre>
83 void f();
85 namespace N {
86 using ::f;
89 void g()
91 using N::f; // C2873: 'f': the symbol cannot be used in a using-declaration
93 </pre>
95 <h3>[explicit-instantiation] Explicit function template instantiation</h3>
97 <p>Trying to explicitly instantiate a function template leads to the wrong
98 function being called silently.</p>
99 <pre>
100 #include &lt;stdio.h&gt;
102 template&lt;class T&gt;
103 void f()
105 printf("%d\n", sizeof(T));
108 int main()
110 f&lt;double&gt;(); // output: "1"
111 f&lt;char&gt;(); // output: "1"
112 return 0;
114 </pre>
116 <h3>[for-scoping] Scopes of definitions in for-loops</h3>
118 <p>The scope of variable definitions in <code>for</code> loops should be
119 local to the loop's body, but it is instead local to the enclosing
120 block.</p>
121 <pre>
122 int main()
124 for(int i = 0; i &lt; 5; ++i)
126 for(int i = 0; i &lt; 5; ++i) // C2374: 'i': Redefinition; multiple initialization
128 return 0;
130 </pre>
132 <p><strong>Workaround:</strong> Enclose the offending <code>for</code>
133 loops in another pair of curly braces.</p>
135 <p>Another possible workaround (brought to my attention by Vesa Karvonen)
136 is this:</p>
137 <pre>
138 #ifndef for
139 #define for if (0) {} else for
140 #endif
141 </pre>
143 <p>Note that platform-specific inline functions in included headers might
144 depend on the old-style <code>for</code> scoping.</p>
146 <h3>[inclass-member-init] In-class member initialization</h3>
148 <p>In-class member initialization, required to implement a
149 Standard-conforming <code>std::numeric_limits</code> template, does not
150 work.</p>
151 <pre>
152 struct A
154 static const int i = 5; // "invalid syntax for pure virtual method"
156 </pre>
158 <p><strong>Workaround:</strong> Either use an enum (which has incorrect
159 type, but can be used in compile-time constant expressions), or define the
160 value out-of-line (which allows for the correct type, but prohibits using
161 the constant in compile-time constant expressions). See <a href=
162 "int_const_guidelines.htm">Coding Guidelines for Integral Constant
163 Expressions</a> for guidelines how to define member constants portably in
164 boost libraries.</p>
166 <h3>[koenig-lookup] Argument-dependent lookup</h3>
168 <p>Argument-dependent lookup, also called Koenig lookup, works for
169 overloaded operators, but not for ordinary functions. No additional
170 namespaces induced from the argument types seem to be considered.</p>
171 <pre>
172 namespace N {
173 struct A {};
174 void f(A);
177 void g()
179 N::A a;
180 f(a); // 'f': undeclared identifier
182 </pre>
184 <h3>[template-friend] Templates as friends</h3>
186 <p>A Template cannot be declared a friend of a class.</p>
187 <pre>
188 template&lt;class T&gt;
189 struct A {};
191 struct B
193 template&lt;class T&gt;
194 friend struct A; // "syntax error"
196 </pre>
198 <h3>[member-template-outofline] Out-of-line definitions of member
199 templates</h3>
201 <p>Defining member templates outside their enclosing class does not
202 work.</p>
203 <pre>
204 template&lt;class T&gt;
205 struct A
207 template&lt;class U&gt;
208 void f();
211 template&lt;class T&gt;
212 template&lt;class U&gt; // "syntax error"
213 void A&lt;T&gt;::f() // "T: undeclared identifier"
216 </pre>
218 <p><strong>Workaround:</strong> Define member templates in-line within
219 their enclosing class.</p>
221 <h3>[partial-spec] Partial specialization</h3>
223 <p>Partial specialization of class templates does not work.</p>
224 <pre>
225 template&lt;class T&gt;
226 struct A {};
228 template&lt;class T&gt;
229 struct B {};
231 template&lt;class T&gt;
232 struct A&lt;B&lt;T&gt; &gt; {}; // template class was already defined as a non-template
233 </pre>
235 <p><strong>Workaround:</strong> In some situations where interface does not
236 matter, class member templates can simulate partial specialization.</p>
238 <h3>[template-value] Dependent template value parameters</h3>
240 <p>Template value parameters whose type depends on a previous template
241 parameter provoke an internal compiler error if the correct syntax (with
242 "typename") is used.</p>
243 <pre>
244 template&lt;class T, typename T::result_type&gt; // C1001: INTERNAL COMPILER ERROR: msc1.cpp, line 1794
245 struct B {};
246 // (omit "typename" and it compiles)
248 </pre>
250 <p><strong>Workaround:</strong> Leave off the "typename" keyword. That
251 makes the program non-conforming, though.</p>
253 <h3>[wchar_t] <code>wchar_t</code> is not built-in</h3>
255 <p>The type <code>wchar_t</code> is not a built-in type.</p>
256 <pre>
257 wchar_t x; // "missing storage class or type identifier"
258 </pre>
260 <p><strong>Workaround:</strong> When using Microsoft Visual C++, the header
261 <a href="../boost/config.hpp">boost/config.hpp</a> includes
262 <code>&lt;cstddef&gt;</code>, which defines <code>wchar_t</code> as a
263 typedef for <code>unsigned short</code>. Note that this means that the
264 compiler does not regard <code>wchar_t</code> and <code>unsigned
265 short</code> as distinct types, as is required by the standard, and so
266 ambiguities may emanate when overloading on <code>wchar_t</code>. The macro
267 <code>BOOST_NO_INTRINSIC_WCHAR_T</code> is defined in this situation.</p>
269 <h3>[delete-const-pointer] Deleting <code>const X *</code> does not
270 work</h3>
272 <p>Trying to delete a pointer to a cv-qualified type gives an error:</p>
273 <pre>
274 void f()
276 const int *p = new int(5);
277 delete p; // C2664: cannot convert from "const int *" to "void *"
279 </pre>
281 <p><strong>Workaround:</strong> Define the function</p>
282 <pre>
283 inline void operator delete(const void *p) throw()
284 { operator delete(const_cast&lt;void*&gt;(p)); }
285 </pre>
287 <p>and similar functions for the other cv-qualifier combinations, for
288 operator delete[], and for the <code>std::nothrow</code> variants.</p>
290 <h2>Standard Library</h2>
292 <h3>[clib-namespace] C library names in global namespace instead of
293 std</h3>
295 <p>Library names from the &lt;c...&gt; headers are in the global namespace
296 instead of namespace std.</p>
298 <p><b>Workaround:</b>&nbsp; The header <a href=
299 "../libs/config/config.htm">boost/config.hpp</a> will define
300 BOOST_NO_STDC_NAMESPACE. It can be used as follows:</p>
301 <pre>
302 # ifdef BOOST_NO_STDC_NAMESPACE
303 namespace std { using ::abs; using ::fabs; }
304 # endif
305 </pre>
307 <p>Because std::size_t and std::ptrdiff_t are so commonly used, the
308 workaround for these is already provided in boost/config.hpp.</p>
309 <hr>
311 <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
312 "http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
313 height="31" width="88"></a></p>
315 <p>Revised
316 <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->04 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38514" --></p>
318 <p><i>Copyright &copy; 2001-2002 <a href="../people/jens_maurer.htm">Jens
319 Maurer</a></i></p>
321 <p><i>Distributed under the Boost Software License, Version 1.0. (See
322 accompanying file <a href="../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
323 at <a href=
324 "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
325 </body>
326 </html>