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

前端 未结 6 868
花落未央
花落未央 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 14:04

    If you're free to turn the function into a template, you can do something like this:

    template 
    struct always_false { static constexpr bool value = false; };
    
    template 
    void never_call_me(Ts&&...)
    {
      static_assert(always_false::value,
                    "You should have never called this function!");
    }
    

    This has the advantage that the compile error will be clean + you can provide an error message. Found here, see that answer for more info on why this works + why always_false is needed.

提交回复
热议问题