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

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...