问题描述
我目前正在重构一些旧代码和我正在开发的项目有一个只使用新的c ++风格的cast的策略。我想确保,我不会错过任何,但目前,我采取的方法是相当粗糙的,所以我想知道是否有任何方式使旧的c风格的cast不在一个c ++项目中编译? (或至少给予编译器警告,如果这是不可能的)
I'm currently refactoring some old code and the project I am working on has a policy of only using the new c++ style casts. I'm trying to make sure that I don't miss any but currently the approach I'm taking is quite crude so I'm wondering if there is any way of making the old c style casts not compile in a c++ project? (or at least give a compiler warning if this is not possible)
推荐答案
如果你使用GCC,添加 -Wold-style-cast
到命令行。这会产生警告,而不是错误,但您可以随时添加 -Werror
,这会将警告(所有警告)变成错误。
If you use GCC, add -Wold-style-cast
to the command line. That gives warnings, not errors, but you can always add -Werror
, which turns warnings (all warnings) into errors.
对于其他编译器,似乎没有其他编译器有这样的警告选项。
As for other compilers, it seems no other compiler has such a warning option.
但是这并不重要:GCC是免费软件,并可用于几乎任何可以区分零和一。只需将其与工作站上的主编译器或连续集成系统一起安装,并仅将其用于此任务。你会发现手边有两个C ++编译器是非常方便的。
But that doesn't really matter: GCC is Free Software, and available on practically anything that can distinguish between zeros and ones. Just install it alongside your main compiler on your workstation, or into your continuous integration system, and use it for this task only. You will find that having two C++ compilers at hand is very convenient in general.
如果安装GCC真的不是一个选择,你可能想偷看at ,其中的一些选项被讨论。
If installing GCC really isn't an option for you, you might want to take a peek at How to find (and replace) all old C-style data type casts in my C++ source code?, where some alternatives are discussed.
这篇关于是否有一种方法禁用c ++中的旧c风格cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!