C++ lambda表达式类中的变量

标签 c++ class gcc c++11 lambda

我想保存 lambda 表达式变量(就像在第一个代码块中一样)。问题是,然后我使用一个类(如第二个代码块),编译器返回一些错误。我不知道如何修复它。

我希望有人能帮助我并解释为什么它不能像这样工作。谢谢。

第一个代码:

// variable for function pointer
void (*func)(int);
// default output function
void my_default(int x) {
    cout << "x =" << "\t" << x << endl << endl;
}


int main() {
    cout << "Test Programm\n\n";

    // 1. Test - default output function
    cout << "my_default\n";
    func = &my_default;
    func(5);

    // 2. Test - special output function 2
    cout << "my_func2\n";
    func = [](int x) {  cout << "x =" << "  " << x << endl << endl; };
    func(5);

    return 0;
}

第二个代码:

class test {
private:
    // variable for function pointer
    void (*func)(int);
    // default output function
    void my_default(int x) {
        cout << "x =" << "\t" << x << endl << endl;
    }

public:
    void dummy(void) {
        // 1. Test - default output function
        cout << "my_default\n";
        func = &my_default;
        func(5);

        // 2. Test - special output function 2
        cout << "my_func2\n";
        func =  [](int x)->int{ cout << "x =" << "  " << x << endl << endl; };
        func(5);
    }
};


// entry
int main() {
    cout << "Test Programm\n\n";

    test a;
    a.dummy();

    return 0;
}

编译器:

pi@raspberrypi ~/dev/property $ gcc -std=c++0x -o test2 test2.cpp -lstdc++
test2.cpp: In member function ‘void test::dummy()’:
test2.cpp:491:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&test::my_default’ [-fpermissive]
test2.cpp:491:17: error: cannot convert ‘void (test::*)(int)’ to ‘void (*)(int)’ in assignment
test2.cpp:496:77: error: invalid user-defined conversion from ‘test::dummy()::<lambda(int)>’ to ‘void (*)(int)’ [-fpermissive]
test2.cpp:496:28: note: candidate is: test::dummy()::<lambda(int)>::operator int (*)(int)() const <near match>
test2.cpp:496:28: note:   no known conversion for implicit ‘this’ parameter from ‘int (*)(int)’ to ‘void (*)(int)’

最佳答案

问题是成员函数不是普通函数,它们不能赋值给函数指针,因为它们所属的类型是它们类型的一部分。 此外,成员函数需要有一个对象被调用,该对象将是函数代码中的this

您有几种解决方案:

  1. 只允许属于你的类的函数

    void (*MyClass::func)(int); // but you can use it only with members of the class
    
  2. 使用 std::function

    typedef std::function<void(int)> func;
    

解决方案 2 是最简单的,因为 std::function 旨在处理任何与模板参数中具有相同签名的可调用对象。 此外,它是唯一允许您存储闭包(来自 lambda 的对象)的解决方案。 参见 C++11 styled callbacks?了解详情。

class test {
private:
    // variable for function pointer
    std::function< void ( int )> func;
    // default output function
    void my_default(int x) {
        cout << "x =" << "\t" << x << endl << endl;
    }

public:
    void dummy(void) {
        // 1. Test - default output function
        cout << "my_default\n";
        func = std::bind(&test::my_default, this, std::placeholders::_1);
        // or 
        func = [&]( int i ){ my_default( i ); };
        func(5);

        // 2. Test - special output function 2
        cout << "my_func2\n";
        func =  [](int x)->int{ cout << "x =" << "  " << x << endl << endl; };
        func(5);
    }
};


// entry
int main() {
    cout << "Test Programm\n\n";

    test a;
    a.dummy();

    return 0;
}

关于C++ lambda表达式类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415862/

相关文章:

gcc - 可以在没有自由的情况下构建 binutils 吗?或者report_times可以禁用吗?

c# - 设备和模拟器上的 UWP 异常

java - java中如何访问外部类中的方法?

python - PyQt - 在窗口之间传递数字变量

python - 递归地创建子类的副本?

c++ - 不一致的完全限定枚举编译时行为

c - uint8 的冗余转换不适用于 GCC 4.8.1

c++ - 二维动态容器qt

c++ - 为什么我对 pointer->next == NULL 的检查在我的链表中出现错误?

c++ - 使用引用和取消引用运算符读取代码行时遇到问题