【发布时间】:2011-02-27 13:08:55
【问题描述】:
当有人可能使用模板元编程来实现目标时,应该考虑哪些经验法则?除了像 boost 这样的库之外,使用模板元编程比普通旧代码更有效的好示例是什么?
【问题讨论】:
标签: c++ templates metaprogramming rules
当有人可能使用模板元编程来实现目标时,应该考虑哪些经验法则?除了像 boost 这样的库之外,使用模板元编程比普通旧代码更有效的好示例是什么?
【问题讨论】:
标签: c++ templates metaprogramming rules
我能想到的一个非常有用的规则是让编译错误尽可能接近“真实”问题。这样,不仅可以更轻松地推断出问题,而且对于使用您的库的其他人来说也更容易推断出问题。
这是我的意思的人为版本:
template<typename Type> struct convert{};
template<> struct convert<double>{ static const int value = D_COORD; };
template<> struct convert<Degree>{ static const int value = ANGLE_COORD; };
template<> struct convert<Radian>{ static const int value = RADIAN_COORD; };
对于尝试convert<int>,如果您只是将第一个声明设置为前向声明,那么它会立即告诉您没有为“定义类型”转换。”
就一个很好的例子而言,恐怕我不得不听从别人的意见。但是,您可能想看看Loki。这不是 Boost,但它确实很棒。
【讨论】: