Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

367
Vistas
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. I can of course simply use auto on its own and the compiler will deduce the correct type. 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. 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. 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?

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);
}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

In C++20 you can constrain it with a concept:

#include <type_traits>

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

void test() {
    auto foo = [](MemberPointer auto memPtr) {};
}
over 4 years ago · Santiago Trujillo Denunciar

0

You can use C++20 requires-clause to do this:

#include <type_traits>

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

Demo

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda