【问题标题】:c++ lambda capture not supported in firebase 5.4.4?firebase 5.4.4 不支持 c++ lambda 捕获?
【发布时间】:2019-01-28 12:50:34
【问题描述】:

我在这里关注官方网站上的 c++ 教程 https://firebase.google.com/docs/auth/cpp/password-auth#register_callback_on_future

在那里,提到不支持 lambda 捕获,因为那里的编译器不支持 std::function。 但是,在这里https://firebase.google.com/support/release-notes/cpp-relnotes 在 2017 年 8 月 23 日发布的 4.1.0 版本中,提到他们增加了对 lambda 捕获的支持。

当我写这样的函数时

void CreateUserInFirebase(const std::string& Email, const std::string& Password)
{
    auth->CreateUserWithEmailAndPassword(Email.c_str(), Password.c_str());

    firebase::Future<firebase::auth::User*> Result = auth->CreateUserWithEmailAndPasswordLastResult();

    Result.OnCompletion([&SomeVariable](const firebase::Future<firebase::auth::User*>& result, void* user_data)
    {}, nullptr);

}

出现这个错误

错误:没有匹配的成员函数调用“OnCompletion”

注意:候选函数不可行:第一个参数没有从 lambda 到 'firebase::Future::TypedCompletionCallback'(又名 'void (*)(const Future &, void *)')的已知转换

是否在最新版本中删除了支持?

谢谢。

【问题讨论】:

    标签: c++ firebase firebase-authentication


    【解决方案1】:

    OnCompletion 有两个重载:

    OnCompletion(TypedCompletionCallback callback, void *user_data) const
    

    OnCompletion(std::function< void(const Future< ResultType > &)> callback) const
    

    您定义了带有两个参数的 lambda,因此编译器选择第一个重载,但 lambda 捕获 SomeVariable - 代码无法编译,带捕获的 lambda 无法转换为指向函数的指针。

    如果你想调用第二个重载,你的 lambda 应该只接受一个参数(user_data 可以在捕获列表中传递)。

    Result.OnCompletion([&SomeVariable](const firebase::Future<firebase::auth::User*>& result) {});
    

    【讨论】:

    • 谢谢你,rafix。我是 Firebase 的新手。我不知道第一次重载正在使用函数指针。在遵循初学者指南的同时,我现在也将使用 API 参考。
    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多