我可以在前向声明类中使用类型吗
Can I use a type in a forward declared class?
这个类有一个枚举:
class ThreadController
{
public:
enum ThreadType { ... }
}
是否可以使用前向声明类中的ThreadType &
?
class ThreadController;
class ThreadWorker
{
public:
static ThreadWorker makeThreadWorker(const ThreadController::ThreadType & type);
}
我得到以下错误:
'ThreadType' in 'class ThreadController' does not name a type
但是,既然我使用的是引用,编译器就不能对头文件中没有定义感到满意吗?
您可以使makeThreadWorker
成为模板化函数。
template <typename T = ThreadController>
static ThreadWorker makeThreadWorker(const typename T::ThreadType & type)
{
}
如果T
不包含ThreadType
,编译器将抛出错误。(可选)添加static_assert
以将T
限制为仅ThreadController
。
static_assert(std::is_same<ThreadController, T>::value, "error");
相关文章:
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- 如何使用自定义类型声明Arduino数组?
- 将函数参数类型声明为 auto
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 在没有标识符的情况下如何读取复杂的C++类型声明?
- 模板函数参数显式类型声明
- 使用指针遍历一系列基元类型声明?
- 字符类型声明和验证
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 为什么 c++ lambda 捕获不需要类型声明?
- 为什么没有"int"类型声明时输出不同?
- C++如何使用虚拟基类型声明全局静态分配的变量
- 我可以使用预处理器将一个类型声明替换为另一个类型声明吗?
- 如何将数据类型声明从.cpp文件传输到 .cu 文件
- C++17 枚举类型声明
- 条件类型声明
- 具有特定参数的特定构造函数的类型声明
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 函数调用之前,C 类型声明