Home > database >  c type trait to detect if any function argument is reference
c type trait to detect if any function argument is reference

Time:04-16

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);
}

CodePudding user response:

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.

  • Related