【问题标题】:Template metaprogramming rules of thumb [closed]模板元编程经验法则[关闭]
【发布时间】:2011-02-27 13:08:55
【问题描述】:

当有人可能使用模板元编程来实现目标时,应该考虑哪些经验法则?除了像 boost 这样的库之外,使用模板元编程比普通旧代码更有效的示例是什么?

【问题讨论】:

    标签: c++ templates metaprogramming rules


    【解决方案1】:

    我能想到的一个非常有用的规则是让编译错误尽可能接近“真实”问题。这样,不仅可以更轻松地推断出问题,而且对于使用您的库的其他人来说也更容易推断出问题。

    这是我的意思的人为版本:

    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&lt;int&gt;,如果您只是将第一个声明设置为前向声明,那么它会立即告诉您没有为“定义类型”转换。”

    就一个很好的例子而言,恐怕我不得不听从别人的意见。但是,您可能想看看Loki。这不是 Boost,但它确实很棒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 2014-01-09
      • 2010-10-28
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多