继承模板化转换运算符
Inheriting a templated conversion operator
考虑以下代码:
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 支持这个:[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);
}
但我认为这是对可能来自概念-TS的语言的扩展。
Is there a working alternative in C++20 to inherit and expose a templated conversion function?
我不知道直接公开它的方法,通过 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...>();
}
};
考虑以下代码:
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 支持这个:[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);
}
但我认为这是对可能来自概念-TS的语言的扩展。
Is there a working alternative in C++20 to inherit and expose a templated conversion function?
我不知道直接公开它的方法,通过 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...>();
}
};