Inheriting a templated conversion operator
Asked Answered
S

2

10

Consider the following code:

template <class R, class... Args>
using function_type = R(*)(Args...);

struct base {
    template <class R, class... Args>
    constexpr operator function_type<R, Args...>() const noexcept {
        return nullptr;
    }
};

struct derived: private base {
    template <class R, class... Args>
    using base::operator function_type<R, Args...>; // ERROR
};

Is there a working alternative in C++20 to inherit and expose a templated conversion function?

Strader answered 24/6, 2020 at 10:25 Comment(4)
Afaics the current C++2a standard draft still contains the passage which blocks using using-declarations to refer to a specialization of a member template conversion function of a base class; eel.is/c++draft/namespace.udecl#4.sentence-2.Gathering
Are there other conversion functions in derived that would otherwise hide the conversion function template in base?Tiossem
@DavisHerring It's trying to change the access.Basketwork
@T.C.: Right—I missed the private inheritance.Tiossem
H
3

GCC support this: [demo]

template <class R, class... Args>
using function_type = R(*)(Args...);

struct base {
    template <class R, class... Args>
    constexpr operator function_type<R, Args...>() const noexcept {
        return nullptr;
    }
};

struct derived: private base {
  
    using base::operator function_type<auto, auto...>; // No error!
};


int main (){
  derived d;
  static_cast <int(*)(int)>(d);
}

But I think this is an extension to the language that may come from the concept-TS.

Hawfinch answered 24/6, 2020 at 10:50 Comment(0)
B
1

Is there a working alternative in C++20 to inherit and expose a templated conversion function?

I don't know a way to directly expose it, through using.

But you can wrap it in a derived operator

struct derived: private base {

    template <typename R, typename... Args>
    constexpr operator function_type<R, Args...>() const noexcept {
       return base::operator function_type<R, Args...>();
    }

};
Brandy answered 24/6, 2020 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.