Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 942
Patrick Döpfner Avatar asked May 05 '26 10:05

Patrick Döpfner


2 Answers

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.

like image 80
Lajos Arpad Avatar answered May 06 '26 23:05

Lajos Arpad


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;
}
like image 34
StoryTeller - Unslander Monica Avatar answered May 06 '26 23:05

StoryTeller - Unslander Monica