Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting a templated conversion operator

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?

like image 483
Vincent Avatar asked Jun 24 '20 10:06

Vincent


2 Answers

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.

like image 146
Oliv Avatar answered Nov 13 '22 04:11

Oliv


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...>();
    }

};
like image 45
max66 Avatar answered Nov 13 '22 03:11

max66