简体   繁体   English

Clang 模板不完整类型

[英]Clang template incomplete type

I have the following code that compiles fine under in Visual Studio and g++ but in Clang I get the error "error: 'ns::B' is an incomplete type"我有以下代码可以在 Visual Studio 和 g++ 中正常编译,但在 Clang 中我收到错误“错误:'ns::B' 是一种不完整的类型”

Ah

#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);
        }

    };
}

Bh

#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;
        }
    };
}

main.cpp主程序

#include "A.h"
#include "B.h"

int main()
{
    ns::A a;
    return a.getB<int>(5);
}

From my understanding the code should work because by the time the template get instantiated, B is complete.根据我的理解,代码应该可以工作,因为当模板被实例化时,B 已经完成。 Is this correct?这样对吗? And if so, is there any way to work around the issue in Clang?如果是这样,有什么办法可以解决 Clang 中的问题?

The program is ill-formed, no diagnostic required.该程序格式错误,无需诊断。

[temp.res]/8 : [临时文件]/8 :

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由于不依赖于模板参数的构造,紧跟其定义的模板的假设实例化将是格式错误的,或
  • [...] [...]

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

相关问题 为什么在模板方法中的clang中检测到不完整的类型? - Why an incomplete type is detected in clang inside a template method? gcc与clang:std :: declval和模板专门化的“无效使用不完整类型” - gcc vs. clang: “invalid use of incomplete type” with std::declval and template specialization 不完整类型适用于 gcc 但不适用于 clang 和 msvc - Incomplete type works with gcc but not with clang and msvc 虽然包含<typeinfo>,但Clang拒绝type_info为不完整 - Clang reject type_info as incomplete although <typeinfo> is included 类中不允许使用不完整类型,但允许在类模板中使用 - Incomplete type is not allowed in a class, but is allowed in a class template 在类模板的成员函数中使用不完整类型 - Using incomplete type in a member function of a class template 模板 class 和“不完整类型的无效使用”错误 - Template class and 'invalid use of incomplete type' error 从不完整类型实例化的模板继承 - Inheriting from template instanciated with incomplete type 为visual studio重构出不完整类型的模板 - Refactoring out template of incomplete type for visual studio 访问模板类时出现“不完整类型”错误 - “incomplete type” error when accessing a template class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM