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 )
{
ASSERT_RETURN_FALSE( nNotZero != 0 );
...
}
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
#ifndef STLHELPER_INCLUDED_
#define STLHELPER_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
#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
#ifdef STL_USING_MAP
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4245)
#pragma warning(disable: 4512)
#pragma warning(disable: 4663)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <map>
#pragma warning(pop)
#endif
#ifdef STL_USING_VECTOR
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4245)
#pragma warning(disable: 4663)
#pragma warning(disable: 4702)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <vector>
#pragma warning(pop)
#endif
#ifdef STL_USING_LIST
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4100)
#pragma warning(disable: 4284)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <list>
#pragma warning(pop)
#endif
#ifdef STL_USING_STRING
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4146)
#pragma warning(disable: 4244)
#pragma warning(disable: 4245)
#pragma warning(disable: 4511)
#pragma warning(disable: 4512)
#pragma warning(disable: 4663)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <string>
#pragma warning(pop)
#pragma warning(disable: 4514)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#endif
#ifdef STL_USING_STREAM
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4097)
#pragma warning(disable: 4127)
#include <sstream>
#include <fstream>
#pragma warning(pop)
#endif
#ifdef STL_USING_MEMORY
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4245)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <memory>
#pragma warning(pop)
#endif
#ifdef STL_USING_STACK
#pragma warning(push)
#include <yvals.h> // warning numbers get enabled in yvals.h
#pragma warning(disable: 4018)
#pragma warning(disable: 4100)
#pragma warning(disable: 4245)
#pragma warning(disable: 4710)
#pragma warning(disable: 4786)
#include <stack>
#pragma warning(pop)
#endif
#ifdef STL_USING_ASSERT
#ifndef ASSERT
#include <cassert>
#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
#endif
#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
#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
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.