删除函数签名类型的成员性? (lambda 的运算符())
Removing member-ness of function signature type? (operator() of lambda)
我希望能够仅使用封装的 lambda 名称而不是硬编码签名来实例化 std::function
:
#include <functional>
auto l = [](bool b){ return b ? 1 : 0; };
std::function<int(bool)> f(l); // XXX - don't want the type hardcoded
// Is something like this possible?
// using sig_t = strip_memberness<decltype(l::operator())>;
// std::function<sig_t> f(l);
对于 std::function<>
参数,有什么方法可以从 l
生成 int(bool)
类型?
即使 C++17 风格的模板参数推导可能在未来使这个特定的用例成为可能,不幸的是,我仅限于 GCC 6/C++14,并且还希望出于其他目的访问该类型比模板实例化。
大致如下:
#include <functional>
template <typename T>
struct MemberToFunction;
template <typename R, typename T, typename ... Args>
struct MemberToFunction<R (T::*)(Args...)> {
using type = R(Args...);
};
template <typename R, typename T, typename ... Args>
struct MemberToFunction<R (T::*)(Args...) const> {
using type = R(Args...);
};
int main()
{
auto l = [](bool b){ return b ? 1 : 0; };
std::function<MemberToFunction<decltype(&decltype(l)::operator())>::type> f(l);
}
我希望能够仅使用封装的 lambda 名称而不是硬编码签名来实例化 std::function
:
#include <functional>
auto l = [](bool b){ return b ? 1 : 0; };
std::function<int(bool)> f(l); // XXX - don't want the type hardcoded
// Is something like this possible?
// using sig_t = strip_memberness<decltype(l::operator())>;
// std::function<sig_t> f(l);
对于 std::function<>
参数,有什么方法可以从 l
生成 int(bool)
类型?
即使 C++17 风格的模板参数推导可能在未来使这个特定的用例成为可能,不幸的是,我仅限于 GCC 6/C++14,并且还希望出于其他目的访问该类型比模板实例化。
大致如下:
#include <functional>
template <typename T>
struct MemberToFunction;
template <typename R, typename T, typename ... Args>
struct MemberToFunction<R (T::*)(Args...)> {
using type = R(Args...);
};
template <typename R, typename T, typename ... Args>
struct MemberToFunction<R (T::*)(Args...) const> {
using type = R(Args...);
};
int main()
{
auto l = [](bool b){ return b ? 1 : 0; };
std::function<MemberToFunction<decltype(&decltype(l)::operator())>::type> f(l);
}