Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

Things become trickier when data has higher priority operators, parenthesis or line breaks in it:

(type*) a(b)->
              c

should become convert_to_type(a(b)->c) not convert_to_type(a)(b)->c etc.

like image 694
Giovanni Funchal Avatar asked Dec 02 '25 12:12

Giovanni Funchal


1 Answers

I'm not aware of a tool that's able to properly parse and replace these automatically.

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.

like image 177
Mark B Avatar answered Dec 04 '25 02:12

Mark B