无警告的 STL






4.91/5 (19投票s)
2001年11月16日
2分钟阅读

197066

775
在警告级别 4 上使用 STL。
引言
我创建了一个特殊的 STL 头文件,因为我总是使用 4 级警告,而包含 STL 内容会产生大量的警告。 经过一番尝试,我发现 STL 本身会开启警告。 这发生在 yvals.h
文件中。 因此,需要一种特殊的包含技术。
目的是将所有警告保留在自己的源代码中,并禁用 STL 警告噪音。 这通过在包含 STL 内容之前关闭警告,然后在之后恢复原始警告状态来完成。
在关闭警告之前,必须包含 yvals.h
文件以激活文件包含保护,并避免进一步更改警告状态。
MSKB 文章“BUG: C4786 警告无法使用 #pragma Warning 禁用”(文章 ID:Q167355)描述了一个无法禁用的警告。 我在使用 map
时遇到了这个问题。 我认为文章示例使用的是一个 tree
,它本身使用了 map
。
断言 (ASSERT) 和 跟踪 (TRACE)
由于我没有使用 MFC,但熟悉了 ASSERT
和 TRACE
,因此我添加了一些定义,以便在 STL 中使用此功能。 这与警告无关,但我觉得非常方便。
您可以使用它,就像在 MFC 中一样。 我扩展了 ASSERT
,以便在发布版本中,当断言为 false 时退出函数。 因此,您可以找到六个额外的断言,例如 ASSERT_RETURN
、ASSERT_RETURN_FALSE
和 ASSERT_RETURN_NULL
,具体取决于函数的返回类型。 您甚至可以使用 ASSERT_BREAK
和 ASSERT_CONTINUE
退出控制结构。
bool function foo( int nNotZero ) { // verify parameter ASSERT_RETURN_FALSE( nNotZero != 0 ); // do anything ... }
用法
我使用宏系统来指定要使用的 STL 元素,或者使用 STL_USING_ALL 来包含所有对象。 STL 头文件并不完整(只是我经常使用的对象),如果需要,必须进行扩展。 您不需要声明 using namespace std;
。 这在头文件的末尾完成。
在您的代码中使用它(我总是将其放在预编译头文件中)
#define STL_USING_ALL #include "STL.h"
STL 头文件
////////////////////////////////////////////////////////////////////// // // 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_
结论
禁用的警告并不完整,并且我会在我做的每个项目中添加新的内容。 我认为这是与 STL 和 4 级警告配合的基本方法。 我欢迎建议和补充,以使其更加完整。