【问题标题】:What is the MPL-value idiom ?什么是 MPL 值成语?
【发布时间】:2014-05-09 07:02:10
【问题描述】:

我正在浏览关于生成式编程的 tutorial,偶然发现了 MPL 值成语

template <int V>
class A
{
    A();
public:
    static const A<V> value;
};

template <int V>
static const A<V> A<V>::value;

它是干什么用的?我可以有一个示例用例吗?

【问题讨论】:

  • static const A&lt;V&gt; A&lt;V&gt;::value;: static 不能在这里使用。

标签: c++ templates metaprogramming


【解决方案1】:

来自您的链接:

防止代码膨胀,只实例化需要的东西。

在幻灯片 35 上,演示者给出了一个示例,其中习语中的 A 类对应于 Reportable 类。

通过这个习语,他实现了只实例化需要的类。比较他之前的示例(幻灯片 32),在该示例中,他必须定义该类的一个对象,无论它是否会被使用。模板化版本不会发生这种情况。

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2010-11-14
    • 2010-09-25
    • 1970-01-01
    • 2012-03-15
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多