Friday 7 August 2009

Suppress Compiler Warning using #pragma

Ocassionally the compiler can throw out warnings which may be informative to you but you do not want others to see. You can use a #pragma directive to suppress the warnings. An example of the code below:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy

//This example shows how to suppress warnings using #pragma

#include<iostream>



using namespace
std;



class
error

{


public
:

error(string s)

{


info = s;

}


private
:

error();

string info;

};




#pragma warning( disable : 4290 )



int
someFunc(void) throw (error)

{


return
1;

}




#pragma warning( default : 4290 )



int
someOtherFunc(void) throw (error)

{


return
1;

}




int
main()

{




return
0;

}




Here, for 'someOtherFunc', the compiler will generate a warning:


warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)


but a similar warning for 'someFunc' wont be generated because we have already suppressed it using the #pragma.


No comments:

Post a Comment