简体   繁体   English

如何将自动 lambda 参数约束为指向成员 function 的指针?

[英]How to constrain an auto lambda parameter to a pointer to member function?

I have a generic lambda function that needs to accept a pointer-to-member-function as a parameter.我有一个通用的 lambda function 需要接受指向成员函数的指针作为参数。 I can of course simply use auto on its own and the compiler will deduce the correct type.我当然可以简单地使用auto ,编译器会推断出正确的类型。 However, where possible, I prefer to decorate my auto parameters with * , & and const where appropriate, thus better communicating the nature and intent of the deduced type.但是,在可能的情况下,我更喜欢在适当的地方用*&const装饰我的自动参数,从而更好地传达推导类型的性质和意图。 If I simply make the auto parameter an auto* , I get a compiler error, which I'm not really surprised by as auto* signifies a regular pointer, not a pointer-to-member.如果我只是将auto参数设为 auto auto* ,则会出现编译器错误,对此我并不感到惊讶,因为auto*表示常规指针,而不是指向成员的指针。 Is there some syntax for constraining an auto parameter to accept a pointer-to-member, or should I just use auto and forget about it?是否有一些语法可以约束auto参数以接受指向成员的指针,或者我应该只使用auto并忘记它?

int main()
{
    struct S { void m() {} };

    //auto l = [](auto* pmf) // ERROR
    //auto l = [](const auto& pmf) // Works, but uh, bit misleading I think
    auto l = [](auto pmf)
    {
        S s;
        (s.*pmf)();
    };

    l(&S::m);
}

You can declare it as:您可以将其声明为:

auto l = [](auto S::*pmf)

It does tie the pointer to a S type, but it makes sense because it is that way you will use it.它确实将指针绑定到S类型,但这是有道理的,因为您将使用它。

In C++20 you can constrain it with a concept:在 C++20 中,你可以用一个概念来约束它:

#include <type_traits>

template <typename T>
concept MemberPointer = std::is_member_pointer_v<T>;

void test() {
    auto foo = [](MemberPointer auto memPtr) {};
}

You can use C++20 requires -clause to do this:您可以使用 C++20 requires -clause 来执行此操作:

#include <type_traits>

auto l = [](auto pmf) requires std::is_member_function_pointer_v<decltype(pmf)>
{
  S s;
  (s.*pmf)();
};

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问作为指针参数的指针传递给 function 的结构成员? - How to access a member of a struct which is passed into a function as pointer to pointer parameter? 成员函数指针作为构造函数参数 - Member function pointer as constructor parameter 如何使用指向朋友函数的指针作为成员函数的参数 - How to use pointer to friend function as a parameter of member function 如何使用指向静态成员函数的函数指针作为模板参数? - How to use a function pointer to a static member function as a template parameter? 如何使用“auto”来声明指向重载成员函数的指针? - How to use 'auto' for declaring a pointer to an overloaded member function? 如何将函数指针传递给重载成员函数作为参数 - How to pass a function pointer to overloaded member function as parameter 如何将 lambda 作为模板类的成员函数的参数 - How to have lambda as member function's parameter of the template class 如何使用C ++ lambda将成员函数指针转换为普通函数指针以用作回调 - How to use a C++ lambda to convert a member function pointer to a normal function pointer for use as a callback 用于分配成员函数指针的 lambda 表达式 - lambda expression to assign a member function pointer 使用C ++ lambda指向成员函数的指针 - Pointer to member function using C++ lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM