Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
c++ type trait to detect if any function argument is reference

I need a type trait to detect if any of the function parameters of a template argument is a reference. This code is working, the trait is "is_any_param_reference" and the static_assert is trigged the signature of foo is changed form void foo( std::string s, int i) to void foo( std::string& s, int i) (first parameter converted to reference).

But id did not work with lambdas... It does not compile if I use:

int main()
{ 
    auto foo2 = [](std::string s, int i){ std::cout << s << " " << i << std::endl; };
    some_function( foo2, s, i  );
}

Any idea of how to generate a type traits that works also for lambda?

Thanks!!

#include <iostream>
#include <string>
#include <type_traits>

using namespace std;

template<typename ... A>
struct any_is_reference : std::false_type {};
template<typename A, typename ... P>
struct any_is_reference<A, P...>: std::conditional_t< std::is_reference<A>::value , std::true_type, any_is_reference<P...> >::type{};

template<typename Sig> struct is_any_param_reference;
template<typename R, typename...Args>
struct is_any_param_reference<R(*)(Args...)>: any_is_reference<Args...>{};

template< typename Sig >
inline constexpr bool is_any_param_reference_v = is_any_param_reference<Sig>::value;

template< typename F , typename ... Args>
void some_function( F f, Args... args)
{
    static_assert(!is_any_param_reference< F >::value, "FUNCTION MUST NOT HAVE PARAMETERS BY REFERENCE");
    
    f(args...);
}

void foo( std::string s, int i)
{
    std::cout << s << " " << i << std::endl;
}

int main()
{
    const std::string s = "Hello";
    int i = 0;
    some_function(foo, s, i);
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

template<typename T> struct is_any_param_reference :
    is_any_param_reference<decltype(&T::operator())>{};

template<typename R, typename...Args>
struct is_any_param_reference<R(*)(Args...)>: any_is_reference<Args...>{};

template<typename T, typename R, typename...Args>
struct is_any_param_reference<R(T::*)(Args...)>: any_is_reference<Args...>{};
template<typename T, typename R, typename...Args>
struct is_any_param_reference<R(T::*)(Args...) const>: any_is_reference<Args...>{};

Demo.

The primary template assumes that its argument is a class providing operator() (e.g. a lambda). Then there are specializations for a plain function pointer as well as a pointer-to-member-function. The primary template effectively delegates to this last specialization.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda