Suppress Compiler warning Function declared never referenced

前端 未结 9 782
你的背包
你的背包 2020-12-30 01:51

So i have some code like this:

void foo (int, int);

void bar ( )
{
    //Do Stuff

   #if (IMPORTANT == 1)
       foo (1, 2);
   #endif

}

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 02:00

    A good way to encapsulate compiler- and system-dependent stuff is to factor it out into headers. Then you adjust the include path depending on the compiler and system and perhaps other things. You can do the same for source code files.

    In this case the declaration doesn't seem to depend on compiler- or system, so just add the following common header:

    // [foo.h]
    #pragma once
    void foo( int, int );
    

    With implementation file

    // [foo.cpp]
    #include 
    

    Then for the build where something should happen, add to the include path a directory containing

    // [foo.virtual.cpp]
    #include 
    void foo( int const a, int const b )
    {
        // Do the thing.
    }
    

    And for the build where nothing should happen, add to the include path a directory containing

    // [foo.virtual.cpp]
    #include 
    void foo( int, int ) {}
    

    If you are afraid that the call of an empty function will be very time consuming, like, a nano-second wasted, then simply move the definitions to headers and add the word inline.

    If foo is also used for other purposes, define a function bar that calls it for the should-or-should-not-happen thing, and do the above for bar instead of for foo.

    Then, you have removed all the preprocessor stuff.

    Remember that preprocessor directives in code are ungood.

提交回复
热议问题