Patrick Döpfner
Patrick Döpfner

Reputation: 51

User-defined conversion-operator in nested class

Why does the following code not compile:

struct X
{
    struct B;

    struct A
    {
        int dummy;
        operator B();
    };

    struct B
    {
        int dummy;
    };
};

X::A::operator B()
{
    B b;
    return b.dummy = dummy, b;
}

My MSVC++ 2017 compiler says:

error C2833: 'operator B' is not a recognized operator or type

Upvotes: 3

Views: 162

Answers (2)

Even though B should be looked up in the scope of X as the user defined conversion operator is being defined, MSVC seems to bungle it up.

You can give it hand by fully qualifying it:

X::A::operator X::B()
{
    B b;
    return b.dummy = dummy, b;
}

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76583

The only possible reason of this error is that struct B is not defined-yet at the point when struct A is being defined. Since the code does not seem the be buggy, my conclusion is that you have found a compiler bug.

Upvotes: 1

Related Questions