[英]get unique return type of template function/generic lambda
有没有一种方法可以获取模板函数/通用lambda的返回类型而无需指定参数类型?
template<class F>
struct unique_result_of {
using type = std::result_of_t<F( *any valid parameter combination that F can be called upon* )>;
}
auto f = [](auto x, auto y, int z) -> int {};
auto g = [](double x, auto y) -> float {};
using return_type_f = typename unique_result_of<decltype(f)>::type; // should be int
using return_type_g = typename unique_result_of<decltype(g)>::type; // should be float
当然,这仅在由相同模板函数名称生成的所有函数确实具有唯一的返回类型时才有效。
您给定的示例适用于以下情况,但有一些警告,如下所述
#include<type_traits>
#include<utility>
struct ubiq
{
template<typename T>
constexpr operator T&() const;
};
template<size_t>
ubiq make_ubiq();
struct broken_t;
template<typename T>
static constexpr bool is_broken_v = std::is_same_v<T, broken_t>;
template<typename T, size_t I0, size_t... I>
auto call(std::index_sequence<I0, I...>)
-> decltype(std::declval<T>()(make_ubiq<I0>(), make_ubiq<I>()...));
template<typename T>
auto call(std::index_sequence<>) -> decltype(std::declval<T>()());
template<typename T, size_t... I>
auto call(std::index_sequence<I...>) -> broken_t;
template<typename T, size_t N>
using call_t = decltype(call<T>(std::make_index_sequence<N>{}));
template<typename Void, typename...>
struct collapse
{
using type = broken_t;
};
template<typename T>
struct collapse<std::enable_if_t<!is_broken_v<T>>, T>
{
using type = T;
};
template<typename T, typename... Ts>
struct collapse<std::enable_if_t<!is_broken_v<T>>, T, broken_t, Ts...> :
collapse<void, T, Ts...> {};
template<typename... Ts>
struct collapse<void, broken_t, Ts...> :
collapse<void, Ts...> {};
template<typename T, typename... Ts>
struct collapse<std::enable_if_t<!is_broken_v<T>>, T, T, Ts...> :
collapse<void, T, Ts...> {};
template<typename... Ts>
using collapse_t = typename collapse<void, Ts...>::type;
template<typename, typename>
struct unique_call;
template<typename T, size_t... Ns>
struct unique_call<T, std::index_sequence<Ns...>>
{
using type = collapse_t<call_t<T, Ns>...>;
};
template<typename T, size_t N = 10>
using unique_call_t = typename unique_call<T, std::make_index_sequence<N>>::type;
以下断言通过
auto f = [](auto x, auto y, int z) -> int {return 42;};
auto g = [](double x, auto y) -> float {return 4.2;};
static_assert(std::is_same_v<int, unique_call_t<decltype(f)>>);
static_assert(std::is_same_v<float, unique_call_t<decltype(g)>>);
这种工作方式是通过“扫描”类型并查看是否可以使用任意数量的参数来调用它。 参数的上限必须预先指定,但是实际上,如果有人给我提供了十个以上的参数,我只是假装它根本不存在:D
然后检查可能的返回类型集,如果其中有不同类型,或者没有,则结果类型将为broken_t
。
struct S
{
int operator()(int);
float operator()(float);
};
struct U {};
static_assert(std::is_same_v<broken_t, unique_call_t<S>>); // passes
static_assert(std::is_same_v<broken_t, unique_call_t<U>>); // passes
此方法无法区分不存在的operator()
和对于相同的参数计数已重载的那个。 以下类型将被视为仅具有int operator()()
。
struct S
{
int operator()();
int operator()(int);
float operator()(float);
};
static_assert(std::is_same_v<int, unique_call_t<S>>); // passes!??
我还没有想到可以做到这一点的方法。
另一个问题是模板
template<typename T, std::enable_if_t<std::is_integral<T>>* = nullptr>
int operator()(T);
由于我们作弊并创建了ubiq
类型并将其用作参数的占位符,因此它不能与模板配合使用,在这种情况下T
不会成为整数。
不幸的是, 目前无法执行此操作 ,因为无法询问编译器开发人员在->
之后以尾随返回类型输入了什么 。 也许反思可以使将来成为可能。
最好的选择是指定参数类型,或者使用可隐式转换为其他任何类型的类型来模拟lambda的调用:
struct any_type
{
template <typename T>
constexpr operator T() { return *this; }
};
这不是完美的,因为如果lambda的主体尝试调用any_type
不“模拟”的方法,则any_type
不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.