为什么此构造函数没有给出不完整的类型错误?

Why is this constructor not giving an incomplete type error?

本文关键字:类型 错误 构造函数 为什么      更新时间:2023-10-16

我有两个文件 test.h 和 main.cpp如下所示:

测试.h

#include <memory>
class TestImpl;
template <typename... T>
void createConnection(T&&... Args)
{
// 1. Why is this working if the constructor is in cpp?
std::unique_ptr<TestImpl> pimpl(new TestImpl(std::forward<T>(Args)...));
std::cout << "Done..." << std::endl;
// 2. Why is this not working if the constructor call has no issues?
pimpl->sayHello();
}

主.cpp

#include <iostream>
#include "test.h"
class TestImpl
{
public:
TestImpl(const std::string& first, const std::string& second)
: _first(first)
, _second(second)
{
}
void sayHello()
{
std::cout << "Hello ... " << std::endl;
}
private:
std::string _first;
std::string _second;
};
int main()
{
std::cout << "Hello World!" << std::endl;
createConnection("ABC", "DEF");
return 0;
}

从评论中可以明显看出,我的主要问题是为什么构造函数调用没有给出错误"无效使用不完整的类型'类 TestImpl'..."。作为参考,我使用的是GCC 5.2,没有特定的标志。

简单地说,GCC不必拒绝你的程序,Clang也不必接受它。它的格式不正确,无需诊断。由于TestImpl不完整,因此您的模板违反了

[温度]/8

。程序格式不正确,无需诊断,如果:

  • 跟在其定义之后的模板的假设实例化将由于构造不 取决于模板参数,或
  • 在假设实例化中对这种构造的解释不同于对 模板的任何实际实例中的相应构造。

有人可能会争辩说被调用的构造函数是依赖的,但类名肯定不是!

在我们的例子中,在模板定义之后立即使用两个字符串包的假设实例化将给出与程序中实例化点不同的结果。这是因为类名本身(同样,不是依赖的(在两个上下文中具有不同的含义。

它不是有效的模板定义。但海湾合作委员会在这里有一些回旋余地,因为不需要诊断,并且继续耕耘。


这在项目符号下的注释中简洁地总结了,虽然不是规范性的,但描述了您的情况:

这可能发生在以下情况下:

  • 非依赖名称中使用的类型在定义模板时不完整,但在定义模板时完成 执行实例化,或