Notes on Microsoft's "visual C++" and "visual studio"
Andrew M. Steane

The following is a loose collection of notes concerning issues I have found less than obvious in using the microsoft visual studio IDE with microsoft visual c++ compiler.

1. Wide char (unicode) verses single-byte char. I mostly don't need wide char, and I don't like the large number of type declarations used in Windows API when often they are a renaming of a standard C++ type. (I can see the reason why they were introduced---to aid future compatibility---but it is a bore learning them). Therefore I like to be able to write a call to MessageBox, for example, like this:

MessageBox(NULL, "There was a problem", "Warning", MB_OK);

I found that visual studio, in its default settings, complained with the compiler error "cannot convert parameter from 'const char *' to 'LPCWSTR'". To solve the problem you can add two type-casts, as in

MessageBox(NULL, (LPCWSTR) "There was a problem", (LPCWSTR) "Warning", MB_OK);

but this is a bore and reduces program readability. To avoid this problem and allow the compiler to accept the first version, you need to tell visual studio not to use a couple of compiler options involving UNICODE. To do this go to

project/properties (or alt-F7);

in the menu system, and find "configuration properties/general". Then find "character set" in the list in the panel, and set it to "not set".

2. valarray.min() and max(). When using valarrays I found the visual c++ compiler could not correctly compile these STL functions! The reason is that somewhere in the windows stuff there is a definition of some macros called min and max, and the compiler thinks this is what you want! I got around the problem by putting

#undef min
#undef max

in a header file. No doubt there are other solutions.

3. String manipulations. The visual c++ compiler did not allow

void myfunc(string s) { blah; }
string name;
myfunc(name + "ab");
The compiler complains that in the last line of code it can't tell what template to use for the "+" operation! Other compilers don't have any trouble with it. One solution is
name += "ab";
if you don't mind modifying the "name" variable.


4. erf, erfc error function. These are not provided in visual c++ version of <math.h>. I think they are not part of the standard, but all the same some compilers do provide them. You can get code to calculate them from various sources, for example Numerical Recipes or the GNU scientific library.

5. multilplying a valarray<double> by an integer  The compiler complained about 8 * f [ slice(...) ] where f is a valarray<double>. It accepted 8.0*f[ slice(...) ].

6. slice_array = slice_array   The following compiled ok but then produced a linker error!

valarray<double> a(1.0,10);
valarray<double> b(2.0,10);
a[ slice(0,3,1) ] = b[ slice(4,3,1) ];
The linker error message was almost impenetrable but appeared to say the = operator was an unresolved external call. I have not looked into this further but it appears to be an incomplete or imperfect implementation of the Standard Template Library. Obviously you can always use for-loops instead of slices, but it is a pity to have to do that.