【问题标题】:C++ template for generating parts of switch statement?用于生成部分 switch 语句的 C++ 模板?
【发布时间】:2010-01-28 19:07:24
【问题描述】:

可以写模板吗

Foo<int n>

这样:

Foo<2>

给予

switch(x) {
  case 1: return 1; break;
  case 2: return 4; break;
}

同时

Foo<3>

给予

switch(x) {
  case 1: return 1; break;
  case 2: return 4; break;
  case 3: return 9; break;
}

?

谢谢!

编辑:

如许多人所猜测的那样,将上面的代码更改为返回正方形(我问得不好)

【问题讨论】:

  • 真的是用来计算平方的吗? (并丢弃它。)
  • 在您的示例中,1、4 和 9 是什么?那应该是开关的“返回值”吗?还是你的意思是别的?
  • C++ 模板经常被比作花哨的宏,通常可以替代某些应用程序的传统宏,但它们不是宏预处理器。此外,由于您的 switch 语句中没有可观察到的行为,因此您发布的示例可能不是您可能真正希望实现的目标的好示例。您可能想要描述您的总体目标是什么 - 可以让模板做您想做的事情,而不是按照您目前可能考虑的方式。
  • 顺便说一句,在return 声明之后你不需要那些break

标签: c++ templates switch-statement


【解决方案1】:

是的,用超大的主模板switch 制作一个模板,并希望/帮助优化器把它变成一个小的switch。请参阅我对您的其他问题的回答Runtime typeswitch for typelists as a switch instead of a nested if's?。另外,请勿重复发布。

【讨论】:

    【解决方案2】:

    如果您打开的值(在本例中为x)在编译时未知,您将无法使用template metaprogramming 来评估switch 的结果。这是因为模板在编​​译时被爆破,而不是在运行时。

    但是,如果您在编译时知道该值,则可以达到类似的效果:

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    template<int V> struct intswitch
    {
        operator int() const
        {
            return V * V;
        }
    };
    
    int main() {
    
        cout << "1 = " << intswitch<1>() << endl
            << "2 = " << intswitch<2>() << endl
            << "3 = " << intswitch<3>() << endl
            << "4 = " << intswitch<4>() << endl
            << "5 = " << intswitch<5>() << endl
            << "6 = " << intswitch<6>() << endl
            << "7 = " << intswitch<7>() << endl
            << "8 = " << intswitch<8>() << endl
            << "9 = " << intswitch<9>() << endl
            << "10 = " << intswitch<10>() << endl
            ;
    }
    

    程序输出:

    1 = 1
    2 = 4
    3 = 9
    4 = 16
    5 = 25
    6 = 36
    7 = 49
    8 = 64
    9 = 81
    10 = 100
    

    【讨论】:

      【解决方案3】:

      排序-a,种类-a,不是真的。尽管没有通过实际开关完成,但您可以获得接近您要求的行为的东西。

      好的,我假设foo&lt;N&gt; 表示能够计算 1 和 N 之间的任何值的平方,但不能计算其他值。所以,我想出了这个:

      template <int t>
      int foo(int x)
      {
          return (x > t)   ? -1 :
                 (x == t)  ? (x * x) :
                             foo<t -1>(x);
      }    
      
      template <>
      int foo<0>(int x)
      {
          return -1;
      }
      

      【讨论】:

      • 运行时复杂度是......糟糕。如果 x &lt; t ,为什么要使用另一个模板?最后,只要x[0,t] 中,你就会得到x*x,不需要在那里递归。
      • 简洁的答案。这就像一个线性查找时间开关,但很聪明。
      【解决方案4】:

      不,您将需要一个查找表来查找类似该伴侣的内容。

      【讨论】:

        【解决方案5】:

        我认为您实际上不是在这里寻找模板,而是在寻找宏。尝试this reference 以获取有关 C 预处理器的信息,可能能够执行您想要的操作。模板适用于类型,不适合您尝试做的事情。

        【讨论】:

        • 我不是 C++ 专家,但我相信模板可以处理值。这就是存在 typename 关键字的原因:创建适用于类型的模板。
        • 确实,例如在编译时计算平方template &lt;int N&gt; struct square { static const int value = N * N; }。它们不会生成正常的开关,尽管可以使用模板元编程设置编译时开关(循环和 if 是可行的,所以我不明白为什么不应该使用开关) - 但它很难看出 OP 首先想要实现什么。
        猜你喜欢
        • 2014-02-06
        • 2015-05-02
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        相关资源
        最近更新 更多