在C ++中通过函数调用替换cast [英] Replace cast by function call in C++
问题描述
重构/替换转换的工具或方法的任何建议,例如:
Any recommendations of a tool or method to refactor/replace casts such as:
(type*) data
into:
convert_to_type(data)
data
有更高优先级的运算符,括号或换行符:
Things become trickier when data
has higher priority operators, parenthesis or line breaks in it:
(type*) a(b)->
c
应成为 convert_to_type(a(b) - > c)
不是 convert_to_type(a)(b) - > c
等。
推荐答案
我不知道有一个工具能够自动正确解析和替换。
I'm not aware of a tool that's able to properly parse and replace these automatically.
意识到是使用g ++与 -Wold-style-cast
这将有助于警告你所有这样的C风格的演员,允许你通过分析代码正确处置问题。这显然比工具需要更多的时间,但它也给你机会审查和防止工具做错误的转换。
The best option I'm aware of is to use g++ with -Wold-style-cast
which will then helpfully warn you for all such C-style casts, allowing you to disposition them properly by analyzing the code in question. This will obviously take more time than a tool but it also give you the opportunity to review and prevent a tool from doing an incorrect conversion.
这篇关于在C ++中通过函数调用替换cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!