Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C++
Article

STL without warnings

Rate me:
Please Sign up or sign in to vote.
4.91/5 (19 votes)
15 Nov 20012 min read 183K   775   60   33
Using STL on warning level 4

Introduction

I started a special STL include file because I always use warning level 4 and including STL stuff flooded me with warnings. After fiddling around for a while I found out that STL itself switches on warnings. This happens in the file yvals.h. Therefore a peculiar including technique is necessary.

The purpose is to keep all warnings in the own source and disable the STL warning noise. This is done by switching off the warnings before you include the STL stuff and restore the original warning state afterwards.

Before switching off the warnings the file yvals.h must be included to activate the files include guard and avoid a further change of the warning state.

MSKB article 'BUG: C4786 Warning Is Not Disabled with #pragma Warning' (Article ID: Q167355) describes a warning that can't be disabled. This occured to me using a map. I think the article sample is using a tree with itself uses a map.

ASSERT and TRACE

Because I'm not using MFC but got acquainted with ASSERT and TRACE I added some defines to have this feature with STL. This is in no relation with the warnings but is quite handy in my opinion.

You can use it like in MFC. I extended ASSERT to quit a function in the release build when the assert is false. Therefore you can find six additional asserts like ASSERT_RETURN, ASSERT_RETURN_FALSE and ASSERT_RETURN_NULL depending on the return type of your funciton. You can even quit control structures with ASSERT_BREAK and ASSERT_CONTINUE.

bool 
function foo( int nNotZero )
{
  // verify parameter
  ASSERT_RETURN_FALSE( nNotZero != 0 );

  // do anything
  ...
}

Usage

I'm using a macro system to specify with STL elements to use or a STL_USING_ALL to include all objects covered. The STL include file is not complete (just the objects I use frequently) and must be extended if necessary. You don't need to declare using namespace std;. This is done at the end of the include file.

To use it in your code (I always place it the precompiled header file):

#define STL_USING_ALL
#include "STL.h"

The STL include file

//////////////////////////////////////////////////////////////////////
//
// Author: Oskar Wieland (oskar.wieland@gmx.de)
//         You can modifiy and distribute this file freely.
//
// Creation: 31.05.2000
//
// Purpose: Declarations for using STL without warning noise.
//
// Usage: Include this file and define at least one of the STL_USING_xxx
//        macros. Currently supported data types from the STL:
//
//        // defines for using the STL
//        #define STL_USING_ALL
//        #define STL_USING_MAP
//        #define STL_USING_VECTOR
//        #define STL_USING_LIST
//        #define STL_USING_STRING
//        #define STL_USING_STREAM
//        #define STL_USING_ASSERT
//        #define STL_USING_MEMORY
//        #define STL_USING_STACK
//
// Sample:
//        #define STL_USING_ALL
//        #include "STL.h"
//
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// include guards
//////////////////////////////////////////////////////////////////////

#ifndef STLHELPER_INCLUDED_
#define STLHELPER_INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif


//////////////////////////////////////////////////////////////////////
// handy define to include all stuff
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_ALL

#define STL_USING_MAP
#define STL_USING_VECTOR
#define STL_USING_LIST
#define STL_USING_STRING
#define STL_USING_STREAM
#define STL_USING_ASSERT
#define STL_USING_MEMORY
#define STL_USING_STACK

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for map
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_MAP

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4018)  // signed/unsigned mismatch
#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4245)  // conversion from 'type1' to 'type2', 
                                // signed/unsigned mismatch
#pragma warning(disable: 4512)  // 'class' : assignment operator could not be generated
#pragma warning(disable: 4663)  // C++ language change: to explicitly specialize 
                                // class template 'vector'
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters in 
                                // the debug information

// BUG: C4786 Warning Is Not Disabled with #pragma Warning
// STATUS: Microsoft has confirmed this to be a bug in the Microsoft product. 
// This warning can be ignored. This occured only in the <map> container.

#include <map>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for vector
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_VECTOR

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4018)  // signed/unsigned mismatch
#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4245)  // conversion from 'type1' to 'type2', 
                                // signed/unsigned mismatch
#pragma warning(disable: 4663)  // C++ language change: to explicitly specialize 
                                // class template 'vector'
#pragma warning(disable: 4702)  // unreachable code
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters
                                // in the debug information
#include <vector>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for list
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_LIST

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4284)  // return type for 'identifier::operator ->' 
                                // is not a UDT or reference 
                                // to a UDT. Will produce errors if applied using 
                                // infix notation
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters 
                                // in the debug information
#include <list>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for string
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_STRING

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4018)  // signed/unsigned mismatch
#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4146)  // unary minus operator applied to unsigned type, 
                                // result still unsigned
#pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', 
                                // possible loss of data
#pragma warning(disable: 4245)  // conversion from 'type1' to 'type2', signed/unsigned 
                                // mismatch
#pragma warning(disable: 4511)  // 'class' : copy constructor could not be generated
#pragma warning(disable: 4512)  // 'class' : assignment operator could not be generated
#pragma warning(disable: 4663)  // C++ language change: to explicitly specialize class 
                                // template 'vector'
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters 
                                // in the debug information
#include <string>

#pragma warning(pop)

#pragma warning(disable: 4514)  // unreferenced inline/local function has been removed
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters 
                                // in the debug information
#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for streams
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_STREAM

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4097)  // typedef-name 'identifier1' used as synonym for 
                                // class-name 'identifier2'
#pragma warning(disable: 4127)  // conditional expression is constant

#include <sstream>
#include <fstream>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for memory
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_MEMORY

// The STL library provides a type called auto_ptr for managing pointers.  
// This template class acts as a stack variable for dynamically allocated 
// memory.  When the variable goes out of scope, its destructor gets called.  
// In its de-structor, it calls delete on the contained pointer, making sure 
// that the memory is returned to the heap.

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4018)  // signed/unsigned mismatch
#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4245)  // conversion from 'type1' to 'type2', 
                                // signed/unsigned mismatch
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' characters 
                                // in the debug information

#include <memory>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for stack
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_STACK

#pragma warning(push)

#include <yvals.h>              // warning numbers get enabled in yvals.h 

#pragma warning(disable: 4018)  // signed/unsigned mismatch
#pragma warning(disable: 4100)  // unreferenced formal parameter
#pragma warning(disable: 4245)  // conversion from 'type1' to 'type2', 
                                // signed/unsigned mismatch
#pragma warning(disable: 4710)  // 'function' : function not inlined
#pragma warning(disable: 4786)  // identifier was truncated to 'number' 
                                // characters in the debug information
#include <stack>

#pragma warning(pop)

#endif


//////////////////////////////////////////////////////////////////////
// STL neccessary declaration for assert
//////////////////////////////////////////////////////////////////////

#ifdef STL_USING_ASSERT

// avoid macro redefinition when using MFC
#ifndef ASSERT

#include <cassert>

// macros for tracking down errors
#ifdef _DEBUG

#define ASSERT( exp )           assert( exp )
#define VERIFY( exp )           assert( exp )
#define TRACE                   ::OutputDebugString

#else

#define ASSERT( exp )           ((void)0)
#define VERIFY( exp )           ((void)(exp))
#define TRACE                   1 ? (void)0 : ::OutputDebugString

#endif  // _DEBUG

#endif  // ASSERT

// additional macros 
#define ASSERT_BREAK( exp )             { ASSERT(exp); if( !(exp) ) break; }
#define ASSERT_CONTINUE( exp )          { ASSERT(exp); if( !(exp) ) continue; }
#define ASSERT_RETURN( exp )            { ASSERT(exp); if( !(exp) ) return; }
#define ASSERT_RETURN_NULL( exp )       { ASSERT(exp); if( !(exp) ) return 0; }
#define ASSERT_RETURN_FALSE( exp )      { ASSERT(exp); if( !(exp) ) return false; }

#endif  // STL_USING_ASSERT


//////////////////////////////////////////////////////////////////////
// verify proper use of macros
//////////////////////////////////////////////////////////////////////

#if defined STL_USING_MAP || defined STL_USING_VECTOR || defined STL_USING_LIST || \
    defined STL_USING_STRING || defined STL_USING_STREAM || defined STL_USING_ASSERT || \
    defined STL_USING_MEMORY || defined STL_USING_STACK
using namespace std;
#else
#pragma message( "Warning: You included <STL.H> without using any STL features!" )
#endif

#endif  // STLHELPER_INCLUDED_

Conclusion

The disabled warnings are not complete and I'm adding new stuff with every project I do. I think this is basic approach to get along with STL and warning level 4. I appreciate suggestions and additions to get it more complete.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerVisual C++ compiler switch for warning levels in external headers Pin
Yuriy Solodkyy21-Dec-17 14:47
Yuriy Solodkyy21-Dec-17 14:47 
GeneralThis file (Stl.h) does not work 100% Pin
Pam G.7-Sep-05 4:01
Pam G.7-Sep-05 4:01 
QuestionWhy disable warnings? Pin
Anonymous21-Jun-05 7:38
Anonymous21-Jun-05 7:38 
AnswerRe: Why disable warnings? Pin
User 58261923-Jan-06 3:58
User 58261923-Jan-06 3:58 
Generalwarning 4018 still appears Pin
Bob Stanneveld6-Oct-04 7:24
Bob Stanneveld6-Oct-04 7:24 
QuestionWhy include &lt;yvals.h&gt; ? Pin
Anonymous15-Jun-04 6:00
Anonymous15-Jun-04 6:00 
Generala working solution (for precompiled headers too) Pin
Anonymous28-Sep-03 15:29
Anonymous28-Sep-03 15:29 
Generala working solution (for precompiled headers too) Pin
Anonymous28-Sep-03 15:24
Anonymous28-Sep-03 15:24 
GeneralSTLFit Pin
Jonathan de Halleux6-Nov-02 4:47
Jonathan de Halleux6-Nov-02 4:47 
GeneralRe: STLFilt Pin
zztopcode29-Mar-05 7:57
zztopcode29-Mar-05 7:57 
GeneralYet another (better?) (working) way Pin
Boye18-Sep-02 0:55
Boye18-Sep-02 0:55 
GeneralRe: Yet another (better?) (working) way Pin
JamieMcLaughlin9-Feb-10 21:51
JamieMcLaughlin9-Feb-10 21:51 
Questionusing namespace? Pin
23-Feb-02 14:11
suss23-Feb-02 14:11 
AnswerRe: using namespace? Pin
jelaft29-Jul-03 13:02
jelaft29-Jul-03 13:02 
GeneralRe: using namespace? Pin
astral-projection24-Mar-05 12:16
astral-projection24-Mar-05 12:16 
Generalanother way Pin
Gautam Sarup29-Nov-01 22:37
Gautam Sarup29-Nov-01 22:37 
GeneralRe: another way Pin
22-Feb-02 3:24
suss22-Feb-02 3:24 
Unfortunately for me, I have tried exactly this and I haven't got it to work.
Bugs in VC6? My incompetence? I don't know.

What I have tried with a lot more success is that I set the warning level to 4 and include all the STL files in stdafx.h (my precompiled header). I then set the warning level for stdafx.cpp only to 3 (or 0, 1 or 2).

My reasoning being this: The files I include in my precompiled header I don't have any control of. Therefore I am not concerned about warnings since I can't modify them anyway. However, my own files I am most concerned in and I wish to compile all of them using warning level 4.


GeneralRe: another way Pin
Gautam Sarup12-Mar-02 20:28
Gautam Sarup12-Mar-02 20:28 
GeneralRe: another way Pin
S Douglas8-Dec-05 16:06
professionalS Douglas8-Dec-05 16:06 
GeneralWarning 4786 CAN be disabled Pin
Andre Gleichner19-Nov-01 0:34
Andre Gleichner19-Nov-01 0:34 
GeneralRe: Warning 4786 CAN be disabled Pin
Patrick Hoffmann20-Nov-01 22:29
Patrick Hoffmann20-Nov-01 22:29 
GeneralRe: Warning 4786 CAN be disabled Pin
DrGary14-Jun-03 16:16
DrGary14-Jun-03 16:16 
GeneralRe: Warning 4786 CAN be disabled Pin
Anonymous11-Feb-04 16:52
Anonymous11-Feb-04 16:52 
GeneralRe: Warning 4786 CAN be disabled Pin
Stein Somers16-Aug-05 23:20
Stein Somers16-Aug-05 23:20 
GeneralRe: Warning 4786 CAN be disabled Pin
Fuzzychaos29-Nov-05 16:44
Fuzzychaos29-Nov-05 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.