英文:
Using `class` keyword followed by an undeclared identifier
问题
以下是翻译好的部分:
// a.hpp
#include <memory>
class A
{
std::unique_ptr<class A_impl> my;
};
// a.cpp
#include "a.hpp"
int main()
{}
问题:
-
<class A_impl>
的语法是什么?在未声明的标识符前面加上class
是什么意思?这被称为什么?它是否在A_impl
上执行了“前向声明”?我还没有提到标识符A_impl
。编译器为什么会接受这个? -
如果这可能与任何“设计模式”有关,请帮助我识别它。
请指出正确的方向。
英文:
The following hpp and cpp files are an excerpt from a large program that I am working with. It will compile with 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:
-
What's the syntax for
<class A_impl>
? What's this (puttingclass
before an undeclared identifier) called? Is it doing a "forward declaration" onA_impl
or what? I haven't said anything about the identifierA_impl
. How come the compiler is okay with that? -
If this happens to be possibly related to any "design pattern", please help me identify it.
Please point out the right direction.
答案1
得分: 6
-
"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
identifiertypeA_impl
. How come the compiler is okay with that?"- 我还没有提到关于
标识符类型A_impl
的任何内容。编译器怎么会没问题呢?
- 我还没有提到关于
-
"
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?"
- 如果这可能与任何 '设计模式' 有关,我该如何识别它?
英文:
> Is it doing a "forward declaration" on A_impl
or what?
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 <s>identifier</s> type 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.
> If this happens to be possibly related to any "design pattern", how can I identify it?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论