c++ - Clang 模板不完整类型

标签 c++ templates clang

我有以下代码,可以在 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 中的问题?

最佳答案

程序格式错误,不需要诊断。

[temp.res]/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
  • [...]

关于c++ - Clang 模板不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701037/

相关文章:

c++ - 头文件和包含最佳实践

c++ - Opencv中的SVM参数优化

templates - 有没有办法在 Liquid 中的空白处拆分文本?

macos - "clang"和 "Apple clang"有什么区别?

c++ - 在带有 libstdc++ 的 Linux 上使用 -fsanitize=memory 和 clang

c++ - 包含 <iosfwd> 似乎会导致命名空间问题?

yaml-cpp - 使用 YAML-CPP 发布解析文件

c++ - 我们应该在何时何地使用编译时宏连接?

c++ - 推导模板化类参数的模板参数 : const issue

c++ - 如何使用 clang 从 TagDecl 获取结构成员