English 中文(简体)
是否使用单体模版?
原标题:Are variadic templates a potential code bloat?
最佳回答

简短的答案是:“你只支付你使用的费用”原则仍然与以前一样适用。

比较两种假设执行(例如,假设执行)的生成代码就可以看出答案的时间越长。

#include <iostream>

template <typename T>
void func1(T& v) {
  v = -10;
}

template <typename T1, typename T2>
void func1(T1& v1, T2& v2) {
  func1(v1); func1(v2);
}

// More unused overloads....
template <typename T1, typename T2, typename T3>
void func1(T1& v1, T2& v2, T3& v3) {
  func1(v1); func1(v2); func1(v3);
}

int main() {
  double d;
  int i;
  func1(d);
  func1(i);
  std::cout << "i=" << i << ", d=" << d << std::endl;
  func1(d,i);
  std::cout << "i=" << i << ", d=" << d << std::endl;
}

有了一位现代的汇编者,如果你想要共同避免模板的话,这大大降低了你撰写的准确数字。 在这种“传统”C++03中,用多种方式(汇编者,而不是关键词)将我版本的“g++”条形形形形形色色,而且没有明显暗示初始化是通过模板功能中的参考进行的。

与同等理论方法相比:

#include <iostream>
#include <functional>

void func1() {
  // end recursion
}

template <typename T, typename ...Args>
void func1(T& v, Args&... args) {
  v = -10;
  func1(args...);
}

int main() {
  double d;
  int i;
  func1(d);
  func1(i);
  std::cout << "i=" << i << ", d=" << d << std::endl;
  func1(d,i);
  std::cout << "i=" << i << ", d=" << d << std::endl;
}

这还产生了几乎相同的编码——某些标签和手提名称与你所期望的不同,但<代码>g++”所生成的 as的分散。 - Wall - Wextra - S (a 4.7 snapshot)没有重大差别。 汇编者基本上撰写了你节目中的所有超载材料,然后像以前那样优化。

以下非模板代码也产生了几乎相同的产出:

#include <iostream>
#include <functional>

int main() {
  double d;
  int i;
  d= -10; i=-10;
  std::cout << "i=" << i << ", d=" << d << std::endl;
  d= -10; i=-10;
  std::cout << "i=" << i << ", d=" << d << std::endl;
}

这里唯一的明显差异是标签和符号名称。

这一点是一个现代的汇编者,可以在模板代码中无一例外地做“什么权利”。 如果你重新表达的是所有模板的简单基调,产出就会简单。 如果不这样,产出就会更实质性,但如果你完全避免模板,产出也会如此。

然而,在这一点令人感兴趣的地方(我认为),我的所有发言都有“有体面的现代汇编者”等资格。 <>如果你重写字典模版,你几乎可以肯定你为汇编<>-<>->而重新使用。 体面的现代编纂者: 没有cl立式老卷编支持单模版。

问题回答

这当然是一个问题。 能够帮助的一件事是排除共同部分:

const char *process(const char *s)
{
  while (*s) {
      if (*s ==  %  && *(++s) !=  % ) {
          ++s;
          return s;
      }
      std::cout << *s++;
  }
  throw std::logic_error("extra arguments provided to printf");
}

template<typename T>
inline const char *process(const char *s,T value)
{
  s = process(s);
  std::cout << value;
  return s;
}

template<typename T, typename... Args>
inline void printf(const char *s, T value, Args... args)
{
  printf(process(s,value),args...);
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签