如果调用某些函数,我可以强制编译器错误? [英] Can I force a compiler error if certain functions are called?

查看:130
本文介绍了如果调用某些函数,我可以强制编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的软件的v1和v2版本。 v1使用注册表保存设置,很多调用GetProfileInt等。v2现在使用sqlite数据库来保存设置。

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.

我们目前正在开发两个分支,将新功能从v1合并到v2分支。我们目前必须记住更新任何注册表调用使用新的配置数据库,这已经错过了几次。

We are currently developing both branches and are merging new features from v1 to the v2 branch. We currently have to remember to update any registry calls to use the new config db and this has been missed a few times.

我想要的是抛出一个编译错误如果在v2中使用了任何GetProfile ...或WriteProfile ...函数。

What I would like is to throw a compiler error if any of the GetProfile... or WriteProfile... functions are used in v2.

我们在Visual Studio 2010中使用C ++。如果没有内置可以我使用脚本的输出以某种方式引发编译器错误?

We're using C++ in Visual Studio 2010. If there's nothing built in can I use the output from a script to throw a compiler error somehow?

推荐答案

由于这个答案被接受,我可能包括解决方案asker实际使用:

Since this answer is accepted I might as well include the solution the asker actually used:

jacobsee发现

jacobsee discovered the deprecated pragma

#pragma deprecated(GetProfileInt)






原始答案:



您可以使用 __ declspec(deprecated)将它们声明为已弃用。它看起来像这样:


Original answer:

You may be able to declare them as deprecated using __declspec(deprecated). It would look like this:

UINT __declspec(deprecated) WINAPI GetProfileInt(
  __in  LPCTSTR lpAppName,
  __in  LPCTSTR lpKeyName,
  __in  INT nDefault
);

您必须从每个翻译单元中包含的标题中执行此操作。这样做会导致在包含已弃用的声明的翻译单元使用该函数时发出警告。

You'll have to do so from a header that is included in every translation unit you care about. Doing so will result in a warning any time a translation unit that includes the deprecated declaration uses that function.

如果您想要编译器错误,将警告作为错误处理,那么你必须打开它,或许修复你已经忽略的所有警告。 (无论您是否使用此解决方案,这些都是好的做法。)

If you want a compiler error and if your project doesn't already treat warnings as errors then you'll have to turn that on, and maybe fix all the warnings you've been ignoring. (These are good practices whether you use this solution or not.)

这篇关于如果调用某些函数,我可以强制编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆