Is it possible to refer to a user-defined conversion template in a using declaration?

Viewed 95

In a class B inheriting from class A, it's possible to use a using declaration to bring members of A into B, even templates, like this:

struct A {
    template <typename T>
    void foo();
};

struct B : private A {
    using A::foo;
};

But can it be done for conversion templates?

struct A {
    template <typename T>
    operator T();
};

struct B : private A {
    using A::operator /* ??? */;
};

There seems to be no way of referring to the template by name, but I would love to be proven wrong or get some clarification.

1 Answers

As a workaround, you can cast to the base class and convert it explicitly:

struct A {
    template <typename T>
    operator T() {
        return T{};
    }
};

struct B : private A {
    template <class T>
    operator T() {
        return static_cast<T>(static_cast<A&>(*this));
    }
};

int main() {
    A a;
    B b;
    int i_a = a;
    int i_b = b;
}
Related