简体   繁体   English

使用 `class` 关键字后跟未声明的标识符

[英]Using `class` keyword followed by an undeclared identifier

The following hpp and cpp files are an excerpt from a large program that I am working with.以下 hpp 和 cpp 文件摘自我正在使用的一个大型程序。 It will compile with g++ -std=c++17 -pedantic -Wall -Wextra .它将使用g++ -std=c++17 -pedantic -Wall -Wextra

// a.hpp

#include <memory>

class A
{
    std::unique_ptr<class A_impl> my;
};
//a.cpp

#include "a.hpp"

int main()
{}

But I don't understand about the syntax on the line regarding the unique pointer.但我不了解有关唯一指针的行上的语法。

Questions:问题:

  1. What's the syntax for <class A_impl> ? <class A_impl>的语法是什么? What's this (putting class before an undeclared identifier) called?这是什么(把class放在一个未声明的标识符之前)叫什么? Is it doing a "forward declaration" on A_impl or what?它是在A_impl上做“前向声明”还是什么? I haven't said anything about the identifier A_impl .我没有说任何关于标识符A_impl How come the compiler is okay with that?为什么编译器可以接受?

  2. If this happens to be possibly related to any "design pattern", please help me identify it.如果这碰巧可能与任何“设计模式”有关,请帮助我识别它。

Please point out the right direction.请指出正确的方向。

Is it doing a "forward declaration" on A_impl or what?它是在A_impl上做“前向声明”还是什么?

Exactly.确切地。 What's probably confusing about it is that it's using an elaborated type specifier in the template argument to do so.可能令人困惑的是,它在模板参数中使用了精心设计的类型说明符来做到这一点。 Differences to a "normal" forward declaration与“正常”前向声明的差异

I haven't said anything about the identifier type A_impl .我没有说任何关于标识符类型A_impl How come the compiler is okay with that?为什么编译器可以接受?

std::unique_ptr can be instantiated with an incomplete type - just like a raw pointer. std::unique_ptr可以用不完整的类型实例化 - 就像原始指针一样。

If this happens to be possibly related to any "design pattern", how can I identify it?如果这碰巧可能与任何“设计模式”有关,我如何识别它?

The PIMPL - "pointer to implementation" idiom. PIMPL - “实现的指针”习语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM