继承模板化转换运算符



请考虑以下代码:

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
};

C++20 中是否有继承和公开模板化转换函数的有效替代方案?

GCC 支持这个: [演示]

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

但我认为这是对可能来自概念 TS 的语言的扩展。

C++20 中是否有继承和公开模板化转换函数的有效替代方案?

我不知道通过using直接暴露它的方法.

但是您可以将其包装在派生运算符中

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

最新更新