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