【发布时间】:2018-12-08 20:21:03
【问题描述】:
我正在尝试在 C++ 中将 lambda 转换为 mem_fn。
我遇到了 gcc 和通用 lambda 的问题。 有谁知道gcc可以做些什么吗?
#include <functional>
// Standard lambda
auto std_lambda = [](double x) -> double {
return 2. * x;
};
// generic lambda
auto generic_lambda = [](auto x) {
auto two = static_cast<decltype(x)>(2.l);
return two * x;
};
void test()
{
// convert lambdas to mem_fn
// this call works on gcc/clang/msvc
auto std_mem_fn = std::mem_fn(
& decltype(std_lambda)::operator() );
// convert lambdas to mem_fn
// this call works on clang/msvc
// but fails on gcc
auto generic_mem_fn = std::mem_fn(
& decltype(generic_lambda)::template operator()<double> );
// By the way, I would be interested to make the
// mem_fn instantiation code more similar
// for generic and template lambdas
}
在 Compiler Explorer 上测试此代码(适用于 clang、mscv,但不适用于 gcc)
【问题讨论】:
-
@MatthieuBrucher:我正在研究如何提取 lambda 类型的概念证明。见github.com/pthom/…