为什么 Visual C++ 缺乏重构功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/363292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:56:46  来源:igfitidea点击:

Why is Visual C++ lacking refactor functionality?

c++visual-studiovisual-studio-2008visual-c++

提问by xyz

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

在 Visual Studio 2008 中用 C++ 编程时,为什么在使用 C# 时在重构菜单中没有看到类似的功能?

I use Renameconstantly 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?

我经常使用重命名,当它不存在时你真的很想念它。我确定您可以获得提供此功能的插件,但为什么在使用 C++ 时未将其集成到 IDE 中?这是由于必须解析 C++ 的一些问题吗?

采纳答案by JSB????

The syntax and semantics of C++ make it incredibly difficultto 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.

C++ 的语法和语义使得正确实现重构功能变得异常困难。可以实现一些相对简单的东西来覆盖 90% 的情况,但是在剩下的 10% 的情况下,这个简单的解决方案会通过更改您不想更改的内容来严重破坏您的代码。

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

阅读http://yosefk.com/c++fqa/defective.html#defect-8以简要讨论 C++ 中的任何重构代码必须处理的困难。

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

微软显然决定为 C++ 开发这个特殊功能,让第三方开发人员尽其所能。

回答by Brian

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.

我不确定为什么会这样,但第三方工具可以提供帮助。例如,现在我正在评估Visual Assist X(由 Whole Tomato 开发)。我们也在使用 Visual Studio 2005。

回答by mem64k

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

devexpress提供插件重构!用于VS2005 和 VS2008 的C++

回答by Binary Worrier

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

不要觉得难以完成,它在 VB.Net 中也不可用:)

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).

与 C# 相比,C++ 是一种难以解析的语言(VB 也是如此,除非您打开了“Option Explicit”和“Option Strict”,否则很难在更大的上下文中准确判断任何代码行正在做什么)。

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.

PS 我将我的答案标记为社区维基,因为我知道它没有提供任何有用的信息。

回答by fukanchik

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

Eclipse 很少进行 C++ 重构,包括“重命名”。在 StackOverflow 上查看这个问题

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

也可以在 Eclipse 中使用 Microsoft 编译器。在这里查看

Try Eclipse and see if it fits for you.

尝试 Eclipse,看看它是否适合您。

回答by Dirk Bester

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

围绕这个问题有很多困惑和困惑。这个惊人的 youtube 视频应该清楚为什么 C++ 重构很难: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.

tl;dr Google 使用允许访问其中间格式的编译器(Clang + LLVM)重构了整个 1 亿行 C++ 代码库。

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...

最重要的是,第三方在这里被搞砸了,除非 MS 以相同的方式输出中间结果,否则他们没有现实的方法来重构 VS C++。如果您从编程问题的角度考虑,这很明显:为了重构 VS C++,您必须能够以与 VS 完全相同的方式编译 C++,并处理相同的错误、限制、缺陷、黑客、快捷方式、变通方法等. 像 Coderush 和 Resharper 这样的常见嫌疑人没有那种疯狂的预算,尽管他们显然正在尝试,但已经好几年了......

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

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

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

2016 年更新:Resharper 现在在 C++ 重构方面做得不错。限制纯粹是针对大型/巨大的项目。

回答by Stepan Yakovenko

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

MS 终于做到了: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.

他们大约在 10 年前就开始这样做了,我记得很久以前看过 ms 频道 9。

回答by Liviu Gheorghisan

I've been using Visual Assist Xwith 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.

我已经在Visual Studio 中使用Visual Assist X大约一年半了。这是一个令人难以置信的工具,可以帮助您处理普通的 C++ 代码,但它在模板化代码上表现不佳。例如,如果您有一个复杂的基于策略的模板设计,它将不知道如何重命名您的变量,并且项目将不再编译。

回答by support_ms

回答by Terrabits

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:

我想指出 Qt Creator(一个与 VC++ 库和构建系统兼容的 C++ IDE)提供了非常有效的符号重命名:

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 - 重构:重命名符号

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 的重命名功能为您提供了它找到的符号引用列表,并有机会在执行替换之前排除它们中的任何一个。所以如果它得到一个错误的符号引用,你可以排除它。

Qt Creator Refactor Replace

Qt Creator 重构替换

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.

所以 C++ 符号重命名是可能的。从 Qt Creator 来到 VS 我感受到了你的痛苦,以至于我已经考虑将相当大的预先存在的 VS 项目转换为使用 Qt Creator。

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.

我不相信这在 C++ 中特别困难的论点。除了它在 Qt Creator 中已经很好地运行这一事实之外,还有一个事实是编译器和链接器可以查找和匹配符号:如果这不可能,您将无法构建您的应用程序。

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++.

事实上,像 Python 这样的动态类型语言也有重命名工具。如果您可以为没有显式引用变量类型的语言创建这样的工具,那么您绝对可以为 C++ 做到这一点。

Case in point:

案例:

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

... Rope,一个 Python 重构库...我尝试了几次重命名,这确实按预期工作。

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

Stack Overflow - 您对 Python 使用哪些重构工具?