gpt4 book ai didi

c++ - 是否有用于模板使用的优化 C++ 编译器?

转载 作者:IT老高 更新时间:2023-10-28 21:39:30 24 4
gpt4 key购买 nike

C++ 模板因其强大的功能而成为我日常工作中的福音。但是不能忽略大量使用模板(hello 元编程和 Boost 库)导致的(非常非常非常长的)编译时间。我已经阅读并尝试了很多手动重组和修改模板代码以使其尽快编译的可能性。

现在我想知道是否有任何 c++ 编译器尝试最小化解释模板类所需的时间。我可能错了,但我觉得我所知道的编译器只是在他们以前的版本中添加了模板解释。

我的问题是:

  • c++ 模板代码是不是很难解释,没有太多需要优化的地方? (我非常怀疑)
  • 是否有真正优化“c++ 模板”解释的 c++ 编译器?
  • 是否有开发新一代 c++ 编译器的项目可以对此进行优化?
  • 如果您要参与这样的项目,您的指导方针是什么?

最佳答案

我希望通过可变参数模板/右值引用来加快编译模板化代码的速度。今天,如果我们想编写在编译时做某事的模板代码,我们就会滥用语言的规则。我们创建了几十个重载和模板特化来产生我们想要的结果,但不是以一种告诉编译器我们的意图的方式。因此,编译器在构建时几乎没有捷径可走。见 Motivation for variadic templates

Are there projects to develop a new generation of c++ compilers that would optimize this ?

是的,有 CLang这是 LLVM 编译器基础架构的 C 语言前端。 CLang 和 LLVM 都是使用 C++ 编码的。 CLang 的开发者中有 Douglas Gregor,他是几个 C++1x 语言提案(如可变参数模板和概念)的作者。作为引用,请参阅 Douglas Gregor 的 clang 针对 GCC 的测试

Here are some quick-n-dirty performance results for template instantiation in Clang and GCC 4.2. The test is very simple: measure compilation time (-fsyntax-only) for a translation unit that computes the Nth Fibonacci number via a template metaprogram. Clang appears to be scaling linearly (or close to it) with the number of instantiations. And, although you can't see it in the chart, Clang is a little over 2x faster than GCC at the beginning (Fibonacci<100>).

CLang 仍在其 early days 中但我认为它有很好的机会成为优秀的 C++ 编译器。

关于c++ - 是否有用于模板使用的优化 C++ 编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/582302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com