简体   繁体   English

在模板中键入独立代码

[英]Type independent code in templates

I am wondering what happens if the compiler instantiates a template function (or methods of a class template). 我想知道如果编译器实例化模板函数(或类模板的方法)会发生什么。 Look at the following (senseless) example: 看下面的(毫无意义的)例子:

template <typename T> T DoSomething(T t)
{
   int i = ToInt<T>(t);
   string s = ToString<T>(t);

   cout << i << endl;
   // ... some more non-type specific code
   cout << s << endl;

   return DoLast<T>(t);
}

Only the first two lines and the last line do something on T taking its actual type into account. 只考虑前两行和最后一行在T上考虑其实际类型。 The code on between isn't type specific. 中间的代码不是特定于类型的。 Now I do this: 现在我这样做:

DoSomething<int>(1);
DoSomething<double>(1);

AFAIK this causes the compiler to instantiate the template two times at compile time which means it creates copies of both, the type specific code and the non-type-specific code. AFAIK这会导致编译器在编译时将模板实例化两次,这意味着它会创建两者的副本,特定于类型的代码和非特定于类型的代码。 However the non-type-specific code doesn't need to be duplicated since it is independent from T. 但是,非类型特定代码不需要复制,因为它独立于T.

Should I optimize this manually, moving the type independent code in a separate function or is there anything in the C++ standard or in compiler optimizers (especially VS) I can rely on? 我应该手动优化它,在单独的函数中移动类型无关的代码,还是C ++标准或编译器优化器(特别是VS)中有什么我可以信赖的?

EDIT: Regarding optimization... I am aware of the time vs memory tradeoff. 编辑:关于优化......我知道时间与内存的权衡。 I assume most optimizers try to balance both according to their settings. 我假设大多数优化器都试图根据它们的设置来平衡两者。 My question mainly is about a big portion of type independent code and a small type specific part where probably an additional call would be a good deal. 我的问题主要是关于大部分类型独立代码和一个小类型特定部分,可能额外的调用将是一个很好的协议。

As with any performance and optimisation question, the only way to get a definite answer for your case is to implement both, profile, and compare the results. 与任何性能和优化问题一样,为您的案例获得明确答案的唯一方法是同时实施,分析和比较结果。

Still, I would say that what you think of as "optimisation" may not actuall be one. 尽管如此,我仍然会说你认为“优化”可能并不是真的。 One of the very important optimisations optimisers do nowadays is call inlining —that's precisely the opposite of what you want to do. 优化者现在做的一个非常重要的优化就是调用内联 - 这恰恰你想要做的相反 Generally, performance is improved by copying function bodies into the call site and eliminating the function call overhead. 通常,通过将函数体复制到调用站点并消除函数调用开销来提高性能。 Of course, this leads to massive code multiplication, but the optimisers know when this matters. 当然,这会导致大量的代码倍增,但优化者知道什么时候这很重要。 Most of the time, the increase in code size does not affect performance at all. 大多数情况下,代码大小的增加根本不会影响性能。

You should look at the the assembly of your compiler compilation to find out what is being done. 您应该查看编译器编译的程序集,以了解正在执行的操作。

Having said that, chances are that the type independent code makes for a standalone reusable package, so anyway it probably makes sense to put it in a separate function whatever the cost or profit could be. 话虽如此,很可能是类型无关的代码生成一个独立的可重用包,所以无论如何,将它放在一个单独的函数中可能是有意义的,无论成本或利润是多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM