Posted on 2007-10-16
Last Modified: 2008-07-10
A program for random number generation which I am looking at includes the following bit:

#include <cmath>
// the basic math functions should be in namespace std
// but aren't in VCPP6
#if !defined(_MSC_VER)
using namespace std;

What is _MSC_VER? Why is this bit of code necessary?
Question by:Rothbard
    LVL 4

    Assisted Solution

    If _MSC_VER is defined, you're compiling under Microsoft Visual C++. More precisely, for Visual C++ 6.0, _MSC_VER will be defined as 1200. See:

    But the #if and #endif really aren't necessary. It won't hurt to compile the "using namespace std;" line in Visual C++ 6.0, even if the math functions aren't in the std namespace.
    LVL 39

    Accepted Solution

    >>>> Why is this bit of code necessary?
    the <cmath> of VC6 was

    // cmath standard header

    #if     _MSC_VER > 1000
    #pragma once

    #ifndef _CMATH_
    #define _CMATH_
    #ifdef _STD_USING
     #undef _STD_USING
     #include <math.h>
     #define _STD_USING
     #include <math.h>
    #endif /* _STD_USING */
    #endif /* _CMATH_ */

     * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED.
     * Consult your license regarding permissions and restrictions.

    You see that <cmath> included (old) math.h in every case, but if _STD_USING was defined by a previous include file, it makes an #undef _STD_USING prior to including math.h.

    That means math.h shouldn't be included with _STD_USING defined in neither case.

    Actually, that still doesn't make much sense in VC6 as <math.h> has no further include and doesn't care for _STD_USING. Same applies for some of the other STL headers that were defined similar. So it seems to me that it was a 'option' rather than a feature. You must know that VC6 was released 1998 a little *before* C++ standard and STL. Nevertheless it has a STL included which redefines some of the symbols defined in the old headers (with extension .h). If not defining 'std' as the default namespace, sources including old headers where redefined symbols were used would not compile as the std:: prefix was missing. As far as I remember that part was incompatible in VC6, i. e. we had to add the 'using namespace std;'  to some sources when compiling them with VC8 though they were compiling without problems in VC6.

    I assume the reason for your codepart was similar. It didn't compile with another compiler because of the missing std:: and so the developer added the 'using namespace std;' in case it was not VC compiler.

    Note, the clause 'using namespace std;' shouldn't be used in header files cause they wouldn't prevent from using alternative template classes by simply changing the default namespace. Also conflicts because of using names which were class or function names in STL may arise if the using clause was switched on by a header that cannot be omitted.

    So, I would remove that sequence in the header you posted.

    Regards, Alex

    Author Comment

    Thank you very much for your replies!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now