我有以下代码,可以在 Visual Studio 和 g++ 中正常编译,但在 Clang 中我收到错误“错误:'ns::B' 是不完整的类型”
啊啊
#pragma once
namespace ns
{
class B;
class A
{
friend class B;
class Inner
{
public:
int x;
Inner(int x) : x(x) {}
};
public:
template<typename T>
T getB(int i)
{
B b = B(Inner(i));
return T(b);
}
};
}
B.h
#pragma once
#include "A.h"
namespace ns
{
class B
{
A::Inner i;
public:
B(A::Inner i) : i(i)
{}
operator int() const
{
return i.x;
}
};
}
主要.cpp
#include "A.h"
#include "B.h"
int main()
{
ns::A a;
return a.getB<int>(5);
}
根据我的理解,代码应该可以工作,因为在实例化模板时,B 已完成。这样对吗?如果是这样,是否有任何方法可以解决 Clang 中的问题?
最佳答案
程序格式错误,不需要诊断。
The program is ill-formed, no diagnostic required, if:
- [...]
- a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, or
- [...]
关于c++ - Clang 模板不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701037/