Can I force a compiler error if certain functions are called?

前端 未结 6 918
花落未央
花落未央 2021-01-11 13:19

I have v1 and v2 versions of my software. v1 uses the registry to save settings, with lots of calls to GetProfileInt, etc. v2 now uses an sqlite db to save settings.

<
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-11 13:42

    The accepted answer is to mark the functions as deprecated, but that doesn't really fit what the question is asking, for two reasons:

    • It only gives a warning, not an error.
    • It will give warnings even if you're using the v1 code.

    There's good reasons to want that, but it's not actually what the original question asks for.

    Luckily, there's a really easy way to get what the questions asks for. The compiler will always throw an error if the function simply doesn't exist. Just throw the functions in an #ifndef.

    #ifndef V2
    
    void GetProfile()
    {
      // Get the profile
    }
    
    void WriteProfile()
    {
      // Write the profile
    }
    
    #endif
    

提交回复
热议问题