78

When programming in C++ in Visual Studio 2008, why is there no functionality like that seen in the refactor menu when using C#?

I use Rename constantly and you really miss it when it's not there. I'm sure you can get plugins that offer this, but why isn't it integrated in to the IDE when using C++? Is this due to some gotcha in the way that C++ must be parsed?

1
  • 1
    refactoring is difficult for C++ but I feel like VS should add a Rename all kind of functionality like the one in Xcode C++. Feb 3, 2016 at 16:06

11 Answers 11

70

The syntax and semantics of C++ make it incredibly difficult to correctly implement refactoring functionality. It's possible to implement something relatively simple to cover 90% of the cases, but in the remaining 10% of cases that simple solution will horribly break your code by changing things you never wanted to change.

Read http://yosefk.com/c++fqa/defective.html#defect-8 for a brief discussion of the difficulties that any refactoring code in C++ has to deal with.

Microsoft has evidently decided to punt on this particular feature for C++, leaving it up to third-party developers to do what they can.

11
  • 8
    No, pretty bad link indeed. The guy is frustrated, and not even polite (contacted the guy once, about some cleaarly abusive points, he did ot care). But yes, refactoring is clearly difficult (depending on code size), and one must think twice on initial design.
    – reder
    Jan 31, 2012 at 15:01
  • 17
    So here we are stating Microsoft is poor in solving incredibly difficult tasks, while the Eclipse Foundation is better? I program in VS for project constraints, but when I need renaming a variale I launch Eclipse just for this...
    – Zac
    Apr 22, 2013 at 10:09
  • 11
    Why is this an accepted answer? Parsing C++ code into a semantically meaningful form HAS to be done by a compiler. MS obviously owns the compiler that is included with their IDE. Why not use the same parsing code? It really doesn't matter how hard parsing is, because they already do it anyhow.
    – Joe
    Apr 19, 2014 at 21:22
  • 4
    That's a bad link, because author lacks knowledge of C++. For example, he's unaware of RVO. Refactoring tools are quite straightforward if you're up to use Clang API, where authors have already gone through all that overload resolution hell. The problem is that MSVS lacks such an API. Oct 27, 2014 at 19:01
  • 2
    Microsoft Visual Studio 2015 is able to refactor rename some of the C++ code.
    – aeroson
    Jan 6, 2016 at 9:31
11

I'm not sure why it is like this, but third-party tools exist that help. For example, right now I'm evaluating Visual Assist X (by Whole Tomato). We're also using Visual Studio 2005.

2
  • Visual Assist X is pretty good and typically does the job pretty well. However, if you get all template meta-programy on it it can throw up its hands on some of your code.
    – Doug T.
    Dec 18, 2008 at 19:24
  • 2
    That's true. I've noticed it doesn't handle more than a few layers of templates very well (like when working with Boost).
    – Brian
    Dec 29, 2008 at 22:19
5

devexpress provides Add-in Refactor! for C++ for VS2005 and VS2008.

1
  • However it doesn't support VS2005 any more. Dec 14, 2011 at 17:10
4

Don't feel hard-done-by, it isn't available in VB.Net either :)

C++ is a HARD language to parse compared with C# (VB too unless you've "Option Explicit" and "Option Strict" switched on, it's difficult to tell exactly what any line of code is doing out of a MUCH larger context).

At a guess it could have something to do with the "difficulty" of providing it.

P.S. I marked my answer as community wiki because I know it's not providing any useful information.

4

Eclipse does few c++ refactorings including 'rename'. Check out this question here on StackOverflow.

It is also possible to use Microsoft compiler with Eclipse. Check out here.

Try Eclipse and see if it fits for you.

3

There is a lot of fud and confusion around this issue. This amazing youtube video should clear up why C++ refactoring is hard: https://www.youtube.com/watch?v=mVbDzTM21BQ

tl;dr Google refactors their entire 100 million line C++ codebase by using a compiler (Clang + LLVM) that allows access to its intermediate format.

Bottom line, third parties are screwed here, there is no realistic way for them to refactor VS C++ unless MS outputs intermediate results the same way. If you think of it from the programming problem perspective this is obvious: in order to refactor VS C++ you have to be able to compile C++ the exact same way VS does with the same bugs, limitations, flaws, hacks, shortcuts, workarounds, etc. The usual suspects like Coderush and Resharper do not have the budget for that kind of insanity although apparently they are trying but it has been years...

http://www.jetbrains.com/resharper-cpp/

Update 2016: Resharper now does a decent job at C++ refactor. Limitations are purely for large / gigantic projects.

2

MS has finally done this: https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-33-C-Refactoring-in-Visual-Studio-2015#time=04m37s

They have started doing this about 10 years ago, I remember watching ms channel9 long ago.

1

I've been using Visual Assist X with visual studio for about one year and a half. It's an incredible tool that helps you a lot with ordinary C++ code, but it doesn't perform very well on templated code. For instance, you if have a sophisticated policy-based template design, it won't know how to rename your variables, and the project won't compile anymore.

0

Install plugin which enables you that functionality: https://visualstudiogallery.msdn.microsoft.com/164904b2-3b47-417f-9b6b-fdd35757d194

1
  • Question relates to VS 2008. This plugin works > VS 2013
    – Kapil Vyas
    Jul 30, 2018 at 20:52
0

I'd like to point out that Qt Creator (a C++ IDE which is compatible with VC++ libraries and build system) provides symbol renaming that works very well:

You can rename symbols in all files in a project. When you rename a class, you can also change filenames that match the class name.

Qt Creator - Refactoring: Renaming Symbols

Qt Creator's rename functionality gives you a list of the symbol references it found and an opportunity to exclude any of them before performing the replace. So if it gets a symbol reference wrong, you can exclude it.

Qt Creator Refactor Replace

So C++ symbol renaming is possible. Coming to VS from Qt Creator I feel your pain, to the point where I've considered converting preexisting VS projects of considerable size to use Qt Creator instead.

I don't buy the argument that this is specifically hard in C++. In addition to the fact that it already works very well in Qt Creator, there's the fact that the compiler and linker can find and match symbols: If that wasn't possible you couldn't build your application.

In fact, languages like Python that are dynamically typed also have renaming tools. If you can create such a tool for a language where there are no explicit references to variable type you can definitely do it for C++.

Case in point:

... Rope, a python refactoring library... I tried it for a few renames, and that definitely worked as expected.

Stack Overflow - What refactoring tools do you use for Python?

-3

Well in spite of comments by all you experts I totally disagree that refactoring support issue has something to do with C++ language semantics or any language semantics for that matter. Except the compiler builder themselves don't choose to implement one in first case due to their own reasons or constraints whatsoever they maybe.

And offense not to be taken but I am sorry to say Mr jsb the above link you provided to support your case (i.e of yosefk) about C++ defect is totally out of question. Its more like you providing direction to "Los angeles" when someone asked for of "San Franisco".

In my opinion raising refactoring difficulty issue for certain language is more like raising a finger on language integrity itself. Especially for languages which is sometimes just pain.... when it comes to their variable declaration and use. :) Okay! tell me how come you loose track of some node within a node tree ... eh? So what it is do with any language be it as simple as machine level code. You know you VS compiler can easily detect if some variable or routine is dead code. Got my point?

About developing third party tool. I think compiler vendors can implement it far more easily and effectively if they ever wanted to then a third party tool which will have to duplicate all the parsing database to handle it. Nowadays compiler can optimize code very efficiently at machine code level and I am hearing here that its difficult to tell how some variable is used previously. You haven't paid any real attention to inner working of compiler I suppose. What database it keep within.

And sure its the almost same database that IDE use for all such similar purposes. In previous time compiler were just a separate entity and IDE just a Text Editor with some specialization but as times goes by the gap between compiler and IDE Editor become less and its directly started working on similar parsed database. Which makes it possible to handle all those intellisense and refactoring or other syntax related issues more effectively. With all precompile things and JIT compiling this gap is almost negligent. So it almost make sense to use same database for both purpose or else your memory demand go higher due to duplication.

You all are programmers - I am not! And you guys seems to be having difficulty visualizing how refactoring can be implemented for C++ or any language that I can't comprehend. Its just all about for something you have to put more effort for some less depending on how heavy is a person you trying push.

Anyway way VS a nice IDE especially when it comes to C#.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.