从函数参数推断模板类型
Infering template type from function argument
我不确定问题的标题,但基本上我很好奇如何创建一个 visitor-like 函数,该函数可以在正确使用的 collection 上对某些类型进行操作类型推断。
例如,collection 包含继承自单个碱基 class (Base
) 的 objects。某些操作仅适用于特定的 child classes(例如,FooBar
继承自 Base
)。
一个实现可以是
template<class T, class F>
void visit(F f)
{
for (auto c : the_collection) {
if (auto t = dynamic_cast<T*>(c)) {
f(t);
}
}
}
这里的问题是调用这样的函数需要指定class类型FooBar
两次:
visit<FooBar>([](FooBar* f) { f->some_method(); });
我想使用类型推断,所以我可以只写 visit([](FooBar* f) ...
,但无法获得正确的模板。
例如:
template<class T>
using Visitor = std::function<void(T*)>;
template<class T>
void visit(const Visitor<T>& f)
{
for (auto c : the_collection)
if (auto t = dynamic_cast<T*>(c))
f(t);
}
适用于 visit<FooBar>([](FooBar*) ...
但不适用于 visit([](FooBar*) ...
。
no matching overloaded function found
void visit(const std::function<void(T *)> &)': could not deduce template argument for 'const std::function<void(T *)> &' from '{....}::<lambda_2c65d4ec74cfd95c8691dac5ede9644d>
是否可以定义一个模板以这种方式推断类型,或者语言规范不允许这样做?
到目前为止我找到的最简单的方法(但不完全是我一直在寻找的)是使用问题中指示的第二种形式定义访问者,并将 lambda 参数声明为auto
:
template<class T>
using Visitor = std::function<void(T*)>;
template<class T>
void visit(const Visitor<T>& f)
{
for (auto c : the_collection)
if (auto t = dynamic_cast<T*>(c))
f(t);
}
// ...
visit<FooBar>([](auto f) { f->some_method(); });
我们可以通过推断lambda的调用运算符的类型来实现visit([](FooBar*){ /*...*/ });
所需的语法。
template <typename Element, typename Class, typename Parameter>
void call(Element element, Class *callable, void(Class::*function)(Parameter) const) {
if (auto parameter = dynamic_cast<Parameter>(element)) {
(callable->function)(parameter);
}
}
template <typename Functor>
void visit(Functor &&functor) {
for (auto element : the_collection) {
call(element, &functor, &Functor::operator());
}
}
您标记了 C++17,因此您可以使用 std::function
的演绎指南。
那么下面的事情呢?
template <typename>
struct first_arg_type;
template <typename R, typename T0, typename ... Ts>
struct first_arg_type<std::function<R(T0, Ts...)>>
{ using type = T0; };
template <typename F>
void visit (F const & f)
{
using T = typename first_arg_type<decltype(std::function{f})>::type;
for (auto c : the_collection)
if (auto t = dynamic_cast<T>(c))
f(t);
}
观察到,您可以在 std::function
中使用标准类型 first_argument_type
而不是自定义类型特征 first_arg_type
,因此
using T = typename decltype(std::function{f})::first_argument_type;
不幸的是,std::function::first_argument_type
从 C++17 开始被弃用,将从 C++20 中删除。
我不确定问题的标题,但基本上我很好奇如何创建一个 visitor-like 函数,该函数可以在正确使用的 collection 上对某些类型进行操作类型推断。
例如,collection 包含继承自单个碱基 class (Base
) 的 objects。某些操作仅适用于特定的 child classes(例如,FooBar
继承自 Base
)。
一个实现可以是
template<class T, class F>
void visit(F f)
{
for (auto c : the_collection) {
if (auto t = dynamic_cast<T*>(c)) {
f(t);
}
}
}
这里的问题是调用这样的函数需要指定class类型FooBar
两次:
visit<FooBar>([](FooBar* f) { f->some_method(); });
我想使用类型推断,所以我可以只写 visit([](FooBar* f) ...
,但无法获得正确的模板。
例如:
template<class T>
using Visitor = std::function<void(T*)>;
template<class T>
void visit(const Visitor<T>& f)
{
for (auto c : the_collection)
if (auto t = dynamic_cast<T*>(c))
f(t);
}
适用于 visit<FooBar>([](FooBar*) ...
但不适用于 visit([](FooBar*) ...
。
no matching overloaded function found
void visit(const std::function<void(T *)> &)': could not deduce template argument for 'const std::function<void(T *)> &' from '{....}::<lambda_2c65d4ec74cfd95c8691dac5ede9644d>
是否可以定义一个模板以这种方式推断类型,或者语言规范不允许这样做?
到目前为止我找到的最简单的方法(但不完全是我一直在寻找的)是使用问题中指示的第二种形式定义访问者,并将 lambda 参数声明为auto
:
template<class T>
using Visitor = std::function<void(T*)>;
template<class T>
void visit(const Visitor<T>& f)
{
for (auto c : the_collection)
if (auto t = dynamic_cast<T*>(c))
f(t);
}
// ...
visit<FooBar>([](auto f) { f->some_method(); });
我们可以通过推断lambda的调用运算符的类型来实现visit([](FooBar*){ /*...*/ });
所需的语法。
template <typename Element, typename Class, typename Parameter>
void call(Element element, Class *callable, void(Class::*function)(Parameter) const) {
if (auto parameter = dynamic_cast<Parameter>(element)) {
(callable->function)(parameter);
}
}
template <typename Functor>
void visit(Functor &&functor) {
for (auto element : the_collection) {
call(element, &functor, &Functor::operator());
}
}
您标记了 C++17,因此您可以使用 std::function
的演绎指南。
那么下面的事情呢?
template <typename>
struct first_arg_type;
template <typename R, typename T0, typename ... Ts>
struct first_arg_type<std::function<R(T0, Ts...)>>
{ using type = T0; };
template <typename F>
void visit (F const & f)
{
using T = typename first_arg_type<decltype(std::function{f})>::type;
for (auto c : the_collection)
if (auto t = dynamic_cast<T>(c))
f(t);
}
观察到,您可以在 std::function
中使用标准类型 first_argument_type
而不是自定义类型特征 first_arg_type
,因此
using T = typename decltype(std::function{f})::first_argument_type;
不幸的是,std::function::first_argument_type
从 C++17 开始被弃用,将从 C++20 中删除。