summaryrefslogtreecommitdiff
path: root/more/microsoft_vcpp.html
blob: 42cb674e0245ad7ade70145d9cbffa826b2f90e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Portability Hints: Microsoft Visual C++ 6.0 SP4</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<table border="1" bgcolor="#007F7F" cellpadding="2">
  <tr>
    <td bgcolor="#FFFFFF"><img src="../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td>
    <td><a href="../index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Home</big></font></a></td>
    <td><a href="../libs/libraries.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>Libraries</big></font></a></td>
    <td><a href="../people/people.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>People</big></font></a></td>
    <td><a href="faq.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>FAQ</big></font></a></td>
    <td><a href="index.htm"><font face="Arial,Helvetica" color="#FFFFFF"><big>More</big></font></a></td>
  </tr>
</table>

<p>

<h1>Portability Hints: Microsoft Visual C++ 6.0 SP4</h1>

Similar to the
<a href="borland_cpp.html">portability hints for Borland C++</a>,
this page provides hints on some language features of the Microsoft Visual C++
version 6.0 service pack 4 compiler.  A list of
acknowledged deficiencies can be found at the
<a href="http://support.microsoft.com/support/kb/articles/q243/4/51.asp">Microsoft support site</a>.
<p>

Each entry in the following list describes a particular issue,
complete with sample source code to demonstrate the effect.
Most sample code herein has been verified to compile with gcc 2.95.2
and Comeau C++ 4.2.44.


<h2>Preprocessor symbol</h2>

The preprocessor symbol <code>_MSC_VER</code> is defined for all
Microsoft C++ compilers.  Its value is the internal version number of the
compiler interpreted as a decimal number.  Since a few other compilers
also define this symbol, boost provides the symbol
<code>BOOST_MSVC</code>, which is defined in
<a href="../boost/config.hpp">boost/config.hpp</a>
to the value of _MSC_VER if and only if the compiler is really
Microsoft Visual C++.

The following table lists some known values.
<p>

<table border="1">
<tr>
<th>Compiler</th>
<th><code>BOOST_MSVC</code> value</th>
</tr>

<tr>
<td>Microsoft Visual C++ 6.0 (up to SP4)</td>
<td>1200</td>
</tr>

</table>


<h2>Core Language</h2>

<h3>[chained using] Chaining <code>using</code>-declarations</h3>

Chaining <code>using</code>-declarations do not work.
<pre>
void f();

namespace N {
  using ::f;
}

void g()
{
  using N::f;  // C2873: 'f': the symbol cannot be used in a using-declaration
}
</pre>


<h3>[explicit-instantiation] Explicit function template
instantiation</h3>

Trying to explicitly instantiate a function template leads to the
wrong function being called silently.

<pre>
#include &lt;stdio.h&gt;

template&lt;class T&gt;
void f()
{
  printf(&quot;%d\n&quot;, sizeof(T));
}

int main()
{
  f&lt;double&gt;();      // output: &quot;1&quot;
  f&lt;char&gt;();        // output: &quot;1&quot;
  return 0;
}
</pre>


<h3>[for-scoping] Scopes of definitions in for-loops</h3>

The scope of variable definitions in <code>for</code> loops should be
local to the loop's body, but it is instead local to the enclosing
block.


<pre>
int main()
{
  for(int i = 0; i &lt; 5; ++i)
   ;
  for(int i = 0; i &lt; 5; ++i)  // C2374: 'i': Redefinition; multiple initialization
   ;
  return 0;
}
</pre>

<strong>Workaround:</strong> Enclose the offending <code>for</code>
loops in another pair of curly braces.


<h3>[inclass-member-init] In-class member initialization</h3>

In-class member initialization, required to implement a
Standard-conforming <code>std::numeric_limits</code> template, does
not work.

<pre>
struct A
{
  static const int i = 5;      // &quot;invalid syntax for pure virtual method&quot;
};
</pre>

<strong>Workaround:</strong> Either use an enum (which has incorrect
type, but can be used in compile-time constant expressions), or define
the value out-of-line (which allows for the correct type, but prohibits
using the constant in compile-time constant expressions).

<h3>[koenig-lookup] Argument-dependent lookup</h3>

Argument-dependent lookup, also called Koenig lookup, works for
overloaded operators, but not for ordinary functions.  No
additional namespaces induced from the argument types seem to be
considered.  

<pre>
namespace N {
  struct A {};
  void f(A);
}

void g()
{
  N::A a;
  f(a);     // 'f': undeclared identifier
}
</pre>


<h3>[template-friend] Templates as friends</h3>

A Template cannot be declared a friend of a class.

<pre>
template&lt;class T&gt;
struct A {};

struct B
{
  template&lt;class T&gt;
  friend struct A;     // &quot;syntax error&quot;
};
</pre>


<h3>[member-template-outofline] Out-of-line definitions of member
templates</h3>

Defining member templates outside their enclosing class does not work.

<pre>
template&lt;class T&gt;
struct A
{
  template&lt;class U&gt;
  void f();
};

template&lt;class T&gt;
template&lt;class U&gt;   // &quot;syntax error&quot;
void A&lt;T&gt;::f()      // &quot;T: undeclared identifier&quot;
{
}
</pre>

<strong>Workaround:</strong> Define member templates in-line within
their enclosing class.


<h3>[partial-spec] Partial specialization</h3>

Partial specialization of class templates does not work.

<pre>
template&lt;class T&gt;
struct A {};

template&lt;class T&gt;
struct B {};

template&lt;class T&gt;
struct A&lt;B&lt;T&gt; &gt; {};  // template class was already defined as a non-template
</pre>

<strong>Workaround:</strong> In some situations where interface
does not matter, member class templates can simulate partial
specialization.


<h3>[template-value] Dependent template value parameters</h3>

Template value parameters whose type depends on a previous template
parameter provoke an internal compiler error if the correct syntax
(with "typename") is used.

<pre>
template&lt;class T, typename T::result_type&gt; // C1001: INTERNAL COMPILER ERROR: msc1.cpp, line 1794
struct B {};
 // (omit &quot;typename&quot; and it compiles)

</pre>

<strong>Workaround:</strong> Leave off the "typename" keyword.  That makes
the program non-conforming, though.


<h3>[wchar_t] <code>wchar_t</code> is not built-in</h3>

The type <code>wchar_t</code> is not a built-in type.

<pre>
wchar_t x;  // &quot;missing storage class or type identifier&quot;
</pre>

<strong>Workaround:</strong> When using Microsoft Visual C++, the
header
<a href="../boost/config.hpp">boost/config.hpp</a>
includes <code>&lt;cstddef></code>, which defines
<code>wchar_t</code> as a typedef for <code>unsigned
short</code>. Note that this means that the compiler does not see
<code>wchar_t</code> and <code>unsigned short</code> as distinct
types, as is required by the standard, and so ambiguities may emanate
when overloading on <code>wchar_t</code>.  The macro
<code>BOOST_NO_INTRINSIC_WCHAR_T</code> is defined in this situation.

<h2>
Standard Library</h2>
<h3>[clib-namespace] C library names in global namespace instead of std</h3>
<p>Library names from the &lt;c...&gt; headers are in the global namespace
instead of namespace std.<p><b>Workaround:</b>&nbsp; The header <a href="../libs/config/index.htm">boost/config.hpp</a>
will define BOOST_NO_STDC_NAMESPACE. It can be used as follows:
<pre># ifdef BOOST_NO_STDC_NAMESPACE
    namespace std { using ::abs; using ::fabs; }
# endif</pre>
<p>Because std::size_t and std::ptrdiff_t are so commonly used, the workaround
for these is already provided in boost/config.hpp.<p>&nbsp;
<hr>

2001-02-01 <a href="../people/jens_maurer.htm">Jens Maurer</a>
</body>
</html>