- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将 lambda 转换为 C++ 中的 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)
最佳答案
这可能是 GCC 错误,因为如果您在构建 std::mem_fun
之前“使用”通用 lambda,它会编译。通过“使用”我的意思是例如调用 lambda 或单独存储 mem-fun 指针:
#include <functional>
auto generic_lambda = [](auto x) {
auto two = static_cast<decltype(x)>(2.l);
return two * x;
};
int main()
{
// call the lambda ...
generic_lambda(1.0);
// or retrieve the mem-fun ptr
auto unused = &decltype(generic_lambda)::template operator()<double>;
// now it compiles on GCC
auto generic_mem_fn = std::mem_fn(
& decltype(generic_lambda)::template operator()<double> );
}
参见 live example
因此您可以使用以下方法使其与 GCC 兼容:
auto ptr = &decltype(generic_lambda)::template operator()<double>;
auto generic_mem_fn = std::mem_fn(ptr);
关于c++ - 带有 gcc 的通用 lambdas mem_fn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53681778/
这是一个后续问题 mem_fn to function of member object 这是当前代码。 #include #include #include struct Int {
KDE/PIM Zanshin 项目在其代码中的多个位置使用了 std::mem_fn,结果证明至少有 1 个版本的 Apple clang(随 OS X 10.9 提供的最新 Xcode 提供的版本
我很困惑为什么需要 std::mem_fn。 我有一个函数接受任何可调用对象(lambda、函数指针等),并将其绑定(bind)到一个参数。 例如: template void Class::DoBi
我正在修补 std::mem_fn,但无法设法将其绑定(bind)到结构成员的数据/函数(更深一层)。 我希望代码能比我描述的更好地说明问题,因为我不熟悉术语。 #include struct In
编译以下代码时,Visual Studio报告: \main.cpp(21): error C2664: 'std::_Call_wrapper,false> std::mem_fn(int Clas
我已经实现了一个C++程序,该程序创建了三个不同的Person对象。这些对象被共享并存储在 vector 中。 函数getVector()以const std::vector>&作为输入并返回std:
我有以下片段: struct Foo { Foo(int num):num_(num){} void print_add(int i) const { std::cout vpf{
我有一个更复杂的包装类版本,它封装了如下用户类型的 std::vector。 struct UserType1Encapsulator { template UserType1Encap
我可以用 C++ 实现以下功能吗?在调用回调方法之前,我想保持未指定 myInstance 变量,而不是将其包含在 boost::bind 实例中。 MyClass *myInstance; void
我有一个带有默认参数的成员函数的类。 struct Class { void member(int n = 0) {} }; 通过 std::tr1::mem_fn 我可以调用它: C
编译以下代码时,Visual Studio 报告: \main.cpp(21): error C2664: 'std::_Call_wrapper,false> std::mem_fn(int Cla
有人可以推荐 tr1 的 mem_fn 和绑定(bind)实用程序的一些很酷的实际用途吗?我不需要深奥的 c++ 来开发库。只是一些利用这些的应用程序级编码。 任何帮助将不胜感激。 最佳答案 我已将
考虑这个人为的例子: template void funny_transform(Iter first, Iter last, vector& v) { transform(first, la
考虑 the following code : struct A { int& getAttribute(); const int& getAttribute() const; };
这是由于 mem_fn() 的实现没有定义 __forceinline/inline/__attribute__((always_inline)) 造成的吗?是否可以解决这个问题,例如使用自己的 me
我正在使用 C++11 和 MSVC2013 尝试在我的类中使用函数指针来拥有自己的成员函数。 class MyClass { public: // ... QColor Co
auto 很好,但我需要在类中声明一个成员,而不是堆栈中的变量。 decltype 有效,但不知何故看起来很奇怪 class Automation { void _init_state(int
我想把这个结果: std::tr1::mem_fn(&ClassA::method); 在一个变量中,这个变量的类型是什么? 看起来像这样: MagicalType fun = std::tr1::m
我理解在其类之外传递成员函数地址的基本问题。我觉得 mem_fn() 可能是解决方案,但我在具体细节上遇到了麻烦。 我在类 p 中有一个成员函数,当前声明为 typedef void (*valNam
我有以下代码: #include struct X { int get() const& { return 42; } }; template std::result
我是一名优秀的程序员,十分优秀!