假设我们有一个枚举类型:

enum DataType { INT, DOUBLE };

和一个类型映射器:
template<DataType T>
struct TypeTraits {};

template<>
struct TypeTraits<INT> { typedef int T; };

template<>
struct TypeTraits<DOUBLE> { typedef double T; };

还有一些代表操作的模板(不要为丑陋的void指针和类似C的类型转换而烦恼):
struct Operation {
  DataType rettype;

  Operation(DataType rettype) : rettype(rettype);
  virtual void* compute();
};

template<DataType RetType>
class Constant : public Operation {
  typedef typename TypeTraits<RetType>::T RType;
  RType val;

  Constant(RType val) : val(val), Operation(RetType) {};
  virtual void* compute(){ return &val; }
};

template<DataType T1, DataType T2, DataType RetType>
class Add : public Operation {
  typedef typename TypeTraits<RetType>::T1 T1Type;
  typedef typename TypeTraits<RetType>::T2 T2Type;
  typedef typename TypeTraits<RetType>::RetType RType;
  RType val;

  Operation *c1, *c2;

  Add(Operation *c1, Operation *c2) : c1(c1), c2(c2), Operation(RetType) {};

  virtual void* compute(){
    T1Type *a = (T1Type *)c1->compute();
    T2Type *b = (T2Type *)c2->compute();
    val = *a + *b;
    return &val;
  }
};

还有一个抽象的树表示形式:
class AbstractNode {
  enum Type { ADD, INT_CONSTANT, DOUBLE_CONSTANT };

  Type type;
  int intval;
  double doubleval;
  child1 *AbstractNode;
  child2 *AbstractNode;
}

我们正在从输入中读取序列化的抽象树,以便将其转换为操作树,然后-计算结果。

我们要写类似:
algebrator(Operation *op){
  if(op->type == AbstractNode::INT_CONSTANT)
    return new Constant<INT>(op->intval);
  else if(op->type == AbstractNode::DOUBLE_CONSTANT)
    return new Constant<DOUBLE>(op->doubleval);
  else {
    Operation *c1 = algebrator(op->child1),
              *c2 = algebrator(op->child2);
    DataType rettype = add_types_resolver(c1->rettype, c2->rettype);
    return new Add<c1->rettype, c2->rettype, rettype>(c1, c2);
  }
}

其中add_types_resolver是根据操作参数类型指定add操作的返回类型的内容。

当然,我们失败了,编译器将打击我们。我们不能将变量用作模板变量!这是因为实例化模板所需的所有信息必须在编译期间可用!

现在-问题。

除了编写大量的if-else或switch-case语句,还有其他解决方案吗?我们不能以任何方式要求编译器在编译过程中扩展所有大小写吗?模板由枚举参数化,因此我们可以保证,此过程是有限的。

而且请-不要写“我认为整个例子搞砸了”这样的回答。我只想知道是否有一种方法来给模板提供变量,因为它是来自有限的小集合。

整个过程似乎有些过头了,但我真的很好奇如何在这种异常情况下实例化类。

最佳答案

带有宏的Quick'n'dirty解决方案:

column_type.cc:

enum ColumnType {
  INT = 1,
  DOUBLE = 2,
  BOOL = 3
};

typed_call_test.cc(用法示例):
#include <iostream>
#include "column_type.cc"
#include "typed_call.cc"

template <ColumnType T>
void PrintType() {
  ::std::cout << T <<::std::endl;
}

int main() {
  ColumnType type = INT;
  // this won't compile:
  // PrintType<type>();
  // and instead of writing this:
  switch (type) {
    case INT:
      PrintType<INT>();
      break;
    case DOUBLE:
      PrintType<DOUBLE>();
      break;
    case BOOL:
      PrintType<BOOL>();
      break;
  }
  // now you can write this:
  TYPED_CALL(PrintType, type, );

  return 0;
}

typed_call.cc(“库”):
// Requirements:
// |function| return type must be void
//
// Usage:
//
// having for instance such |type| variable:
//   ColumnType type = INT;
// and such |foo| function definition:
//   template <ColumnType T>
//   void foo(t1 arg1, t2 arg2) {
//     …
//   }
//
// instead of writing (won't compile):
//   foo<type>(arg1, arg2);
// write this:
//   TYPED_CALL(foo, type, arg1, arg2);
//
//
// for foo with 0 arguments write this:
//   TYPED_CALL(foo, type, );
//
#define TYPED_CALL(function, type, args...) {                        \
  switch (type) {                                                    \
    case INT:                                                        \
      function<INT>(args);                                           \
      break;                                                         \
    case DOUBLE:                                                     \
      function<DOUBLE>(args);                                        \
      break;                                                         \
    case BOOL:                                                       \
      function<BOOL>(args);                                          \
      break;                                                         \
  }                                                                  \
}

#define BASE_TYPED_CALL(function, type, args...) {                   \
  switch (type) {                                                    \
    case INT:                                                        \
      function<int>(args);                                           \
      break;                                                         \
    case DOUBLE:                                                     \
      function<double>(args);                                        \
      break;                                                         \
    case BOOL:                                                       \
      function<bool>(args);                                          \
      break;                                                         \
  }                                                                  \
}

要使此解决方案“升级”,您可以将宏替换为一个函数(仍然包含类似的开关构造)。但是可能您想将函子(带有()运算符的对象)作为此函数的参数而不是像此宏中的普通函数一样传递。顺便说一句:这正是他们在Google中的做法。

第一个旁注:您好,我的同学在华沙大学的Columnar and Distributed DataWarehouses类(class)中!这门类(class)产生了许多令人费解的C++模板问题:)

第二个旁注:这是我相当于Typetraits的样子:
template <ColumnType T>
struct EnumToBuiltin {
};

template <>
struct EnumToBuiltin<INT> {
  typedef int type;
};
template <>
struct EnumToBuiltin<DOUBLE> {
  typedef double type;
};
template <>
struct EnumToBuiltin<BOOL> {
  typedef bool type;
};


template <typename T>
struct BuiltinToEnum {
};

template <>
struct BuiltinToEnum<int> {
  static const ColumnType type = INT;
};
template <>
struct BuiltinToEnum<double> {
  static const ColumnType type = DOUBLE;
};
template <>
struct BuiltinToEnum<bool> {
  static const ColumnType type = BOOL;
};

09-10 04:39
查看更多