1

Is there some tool that can automatically convert the following c style code

A *a = b;

to

A *a = (A*)b;

Thanks, James

Alex B
  • 82,554
  • 44
  • 203
  • 280
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
  • While you're converting C-style code to C++, you might as well use C++ style casts as well: A a* = static_cast(b); – Eclipse Aug 27 '09 at 21:38
  • Almost a dupe of http://stackoverflow.com/questions/1272570/how-to-find-and-replace-all-old-c-style-data-type-casts-in-my-c-source-code – Diaa Sami Aug 27 '09 at 21:41
  • I think that question is asking about if it's possible to replace (A*) with static_cast – jameszhao00 Aug 27 '09 at 21:47

1 Answers1

2

Assuming this is to eliminate compiler errors, I would probably write one myself. Run the compiler on the source, and redirect error messages to a file. Filter out the errors where it complains about the type. For example, in gcc, they will look like this:

a.cc:3: error: invalid conversion from ‘int’ to ‘int*’

This gives you all you need: file and line number, as well as the type you need to cast to (i.e. int*). Find a likely place in the line to insert the cast (i.e. after the = character, or after the return statement), and try again. Keep track of the lines that you already edited, and skip them for human intervention.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • Yea that's a good suggestion. I'm looking for some preexisting tool at the moment, though, before I go and roll my own. – jameszhao00 Aug 27 '09 at 21:49