Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c++ - 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"

A.h

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

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. Is this correct? And if so, is there any way to work around the issue in Clang?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The program is ill-formed, no diagnostic required.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...