【问题标题】:What does it mean when using the template in C++在 C++ 中使用模板是什么意思
【发布时间】:2010-11-04 06:37:51
【问题描述】:

最近,我在别人的源代码中遇到了一些东西。我不太了解 C++ 中的模板。你能帮我吗?

struct my_grammar : public grammar<my_grammar>
{
    ...
};

为什么my_grammar可以像类型参数一样使用呢?

最好的问候,

【问题讨论】:

  • 语言是 C++,不是 CPP。 CPP 通常表示“C 预处理器”。

标签: c++ templates metaprogramming


【解决方案1】:

这是一个称为奇怪重复模板模式的习语 - 请参阅http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern。总而言之,它提供派生类作为基类的策略,其风格类似于 Alexandrescu 的 Modern C++ Design book 中使用的 Policy(强烈推荐)。这样,基类就可以使用派生类的各个方面——类型、常量、方法——所有这些都在编译时解决。

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 2016-12-08
    • 2015-11-03
    • 1970-01-01
    • 2022-09-23
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多