提取“invokables”的签名

时间:2016-04-08 07:29:01

标签: c++ c++14

我在提取函数,成员函数和lambda的签名时遇到问题。我已经编写了下面的代码来做这件事,但我想要一个元函数来做同样的事情,如果可能的话。现有解决方案的问题是,lambdas不能是模板参数。

template <typename R, typename ...A>
struct signature
{
};

template <typename R, typename ...A>
constexpr auto num_args(signature<R, A...> const)
{
    return sizeof...(A);
}

template <typename R, typename ...A>
constexpr auto extract_signature() noexcept
{
  return signature<R, A...>();
};

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...)) noexcept
{
  return extract_signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature(R (C::* const)(A...) const) noexcept
{
  return extract_signature<R, A...>();
}

template <typename R, typename ...A>
constexpr auto extract_signature(R (*const)(A...)) noexcept
{
  return extract_signature<R, A...>();
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
  decltype(&F::operator(), extract_signature(&F::operator()))
{
  return extract_signature(&F::operator());
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
  decltype(f(), extract_signature(f))
{
  return extract_signature(f);
}

我正在寻找类似的东西:

template <???>
struct signature_info
{
  using type = signature<R, A...>;
};

元函数应该以某种方式提取RA...,如果可能的话,可能是decltype(lambda)

编辑:

::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;

会产生错误:

t.cpp:54:85: error: a lambda expression may not appear inside of a constant expression
  ::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;
                                                                                    ^
t.cpp:54:58: error: non-type template argument is not a constant expression
  ::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;
                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp:54:85: note: non-literal type '(lambda at t.cpp:54:85)' cannot be used in a constant expression
  ::std::cout << ::std::integral_constant<::std::size_t, num_args(extract_signature([](int, int, int){}))>{} << ::std::endl;

1 个答案:

答案 0 :(得分:1)

给出最后列出的代码段。

这可以通过与std::function<>的工作方式类似的方式完成......

template <typename S>
struct signature_info

template <typename R, typename A...>
struct signature_info<R(A...)>
{
  using type = signature<R, A...>;
  static const std::size_t count = sizeof...(A);
};

上述专业化可以扩展为支持您希望支持的所有功能类型。

例如,对于简单的const成员函数;

template <typename C, typename R, typename... A>
struct signature_info<R(C::*)(A...) const>
{
  using type = signature<R, A...>;
  static const std::size_t count = sizeof...(A);
};

要获取参数A...的计数,您可以使用sizeof...(A..)

使用元函数...

template <typename R, typename ...A>
struct signature
{
};

template <typename S>
struct signature_info;

template <typename R, typename... A>
struct signature_info<R(A...)>
{
  using type = signature<R, A...>;
};

template <typename S>
signature_info<S> get_signature()
{
    return signature_info<S>{};
}

void func(int);

int main()
{
    auto abc = get_signature<decltype(func)>();
    cout << abc.count << endl;
    return 0;
}

组合OP代码的完整工作样本(删除了递归)和上面的代码can be found here

鉴于代码片段和编译器错误 - 在需要constexpr(核心常量表达式)的情况下不能使用lambdas(尽管在这方面存在建议的更改)。

可以将代码修改为以下内容以便编译成功;

auto lambda = [](int, int, int){};
std::cout << ::std::integral_constant<::std::size_t, decltype(extract_signature(lambda))::count>{} << ::std::endl;

使用完整working sample here;

#include <iostream>
#include <type_traits>
#include <utility>

using namespace std;
template <typename R, typename ...A>
struct signature
{
  static const std::size_t count = sizeof...(A);
};

template <typename R, typename ...A>
constexpr auto extract_signature1() noexcept
{
  return signature<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature2(R (C::* const)(A...)) noexcept
{
  return extract_signature1<R, A...>();
}

template <typename C, typename R, typename ...A>
constexpr auto extract_signature2(R (C::* const)(A...) const) noexcept
{
  return extract_signature1<R, A...>();
}

template <typename R, typename ...A>
constexpr auto extract_signature(R (*const)(A...)) noexcept
{
  return extract_signature1<R, A...>();
}

template <typename F>
constexpr auto extract_signature(F const& f) noexcept ->
  decltype(&F::operator(), extract_signature2(&F::operator()))
{
  return extract_signature2(&F::operator());
}

int main()
{
    auto lambda = [](int, int, int){};
    std::cout << ::std::integral_constant<::std::size_t, decltype(extract_signature(lambda))::count>{} << ::std::endl;
}