Posts Tagged ‘VC9’

How to deprecate a function?

August 4, 2008 Leave a comment

Well what is deprecation? It’s just a way to tell the user that a better/safer function is available for a function which he is using!

For e.g. strcpy.

If you use strcpy in VC8 we’ll get a warning saying that it’s deprecated, instead use strcpy_s which is the secure version of this API.

So the question is how to deprecate a function using VC++ compiler. There are two options in VC++…

  1. Using __declspec(deprecated)
  2. Using #pragma deprecated

Using __declspec(deprecated) compiler directive.

// Helper #defines for deprecation compiler directive
#define deprecate __declspec(deprecated)
#define deprecate_msg(message) __declspec(deprecated(message))

First version is just plain, just produces default compiler warnings, while second version prints out a custom message along with the normal deprecation message.

// Deprecate a function
void deprecate OldFunction() {}

// Deprecate a class/struct
class deprecate OldClass {};

// This time with custom message //

// Deprecate a function, also tell the user that a better option exists
deprecate_msg( "******OldFunction is deprecated, start using NewFunction******" )
// Deprecate a class/struct, also tell the user that a better option exists
deprecate_msg( "******OldClass is deprecated, start using NewClass******" )

The output that I get looks like this

\consolegunieapig\consolegunieapig.cpp(144) : warning C4996: ‘OldFunction’ was declared deprecated
1> \consolegunieapig\consolegunieapig\consolegunieapig.cpp(124) : see declaration of ‘OldFunction’
1>        Message: ‘******OldFunction is deprecated, start using NewFunction******’
1>\consolegunieapig\consolegunieapig\consolegunieapig.cpp(145) : warning C4996: ‘OldClass’ was declared deprecated
1>\consolegunieapig\consolegunieapig\consolegunieapig.cpp(128) : see declaration of ‘OldClass’
1>        Message: ‘******OldClass is deprecated, start using NewClass******’

Using #pragma deprecated

Another option to deprecate a class/struct/function is to use the #pragma deprecated compiler directive.

The main difference is that using __declspec(deprecated) we can selective deprecate functions but with #pragma deprecated every function with that particular name is marked as deprecated. With #pragma deprecated you can even deprecate a macro.

This is how we use #pragma deprecated

void OldFunction() {}
void OldFunction( int i ) {}
class OldClass {};

// Note that both versions of OldFunction will be marked as deprecated.
#pragma deprecated(OldFunction,OldClass);

How to get vc compiler version?

August 1, 2008 Leave a comment

Having trouble finding out your VC++ compiler version number? Let me help you out. 🙂 Open command prompt type and type in

cl.exe /?

Relevant part, as shown for me, is pasted here. My current compiler is VC6.

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80×86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Important pieces are in bold. Compiler is a 32-bit one and it’s version is 12.00.8804. So _MSC_VER will be 1200. Also the copyright year is important 1984-1998, this compiler was released in 1998, 1984 tells the year when Microsoft(R) Corp was registered as a company. 😉

So if you want to compile some piece of code just for VC6 compiler then you can add conditional compilation statements likewise…

#if _MSC_VER == 1200
  // Some VC6 specific code

For VC8, above copyright message looks like this for me…

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80×86 Copyright (C) Microsoft Corporation.  All rights reserved.

So _MSC_VER for VC8 will be 1400.

Now you may ask that how can I check full version of my vc++ compiler, for that purpose we have _MSC_FULL_VER macro defined.

#if _MSC_FULL_VER == 140050727
std::cout << "You are using vc8 compiler, version: " << _MSC_FULL_VER; #endif[/sourcecode] Values of _MSC_VER for different VC versions are listed below... VC6 - 1200 VC7 - 1300 VC8 - 1400 VC9 - 1500

Enum bit flags visualization in VS2008 debugger!

%d bloggers like this: