gpt4 book ai didi

c++ - 在 VS 2010 上临时接受回调

转载 作者:行者123 更新时间:2023-11-30 03:09:01 25 4
gpt4 key购买 nike

我有一个使用右值引用存储参数的回调实现,它可以很好地与 gcc 一起工作,但无法在 VS 2010 中编译某些代码。一个简短的版本:

#include <iostream>
#include <string>

class B {
public:
virtual void execute() = 0;
};

template<typename FuncType, typename ArgType>
class F : public B {
public:
F(FuncType func, ArgType && arg) : f(func), arg(arg) {}
void execute() { f(arg); }
private:
FuncType f;
ArgType && arg;
};

template<typename FuncType, typename ArgType>
B * registerFunc(FuncType func, ArgType && arg)
{
return new F<FuncType, ArgType>(func, arg);
}

void myFunction(std::string text)
{
std::cout << "Function " << text << " here" << std::endl;
}

int main()
{
const char text1[] = "sample1";
std::string text2("sample2");

B * b = registerFunc(myFunction, text1);
b->execute();
delete b;

b = registerFunc(myFunction, text2);
b->execute();
delete b;

// VS 2010 fails because of this call
b = registerFunc(myFunction, text2.c_str());
b->execute();
delete b;

return 0;
}

对于 gcc 4.4 这会产生:

$ g++ clbck.cpp -std=c++0x -o clbck && ./clbck
Function sample1 here
Function sample2 here
Function sample2 here

但是,由于标记行,尝试实例化 registerFunc 时无法在 VS 2010 中编译:

error C2664: 'F::F(FuncType,ArgType &&)' : cannot convert parameter 2 from 'const char *' to 'const char *&&'
with
[
FuncType=void (__cdecl *)(std::string),
ArgType=const char *
]
You cannot bind an lvalue to an rvalue reference

谷歌搜索在 VS2010 上发现了 Boost 1.44 的类似错误,但推荐的解决方案是根本不使用右值引用。真的没有别的办法了吗?

当您看到它时,我处理这些回调的方式有问题吗?它适用于函数指针和仿函数(我还没有测试 lambda),我发现的唯一缺点就是上面描述的缺点。 (请记住,这里显示的代码只是一个小演示,在实际代码中我没有给用户任何指示;我实际上是用它在 Qt 应用程序的不同线程中执行函数)。

最佳答案

我相信 Visual Studio 的提示是对的,你可以找到解释 here .

registerFunc 中,表达式 arg 是一个左值(它有一个名字)。如果你想转发它作为右值引用,你必须使用std::forward:

template<typename FuncType, typename ArgType>
B * registerFunc(FuncType func, ArgType && arg)
{
return new F<FuncType, ArgType>(func, std::forward<ArgType>(arg));
}

同样的问题发生在 FuncType 构造函数中,但是添加一个 std::forward 会产生一个有趣的警告:

reference member is initialized to a temporary that doesn't persist after the constructor exits

不幸的是,我没有足够的右值引用来进一步帮助你,但我怀疑右值引用成员是否有意义:右值引用绑定(bind)到即将消亡的对象,存储它是否有意义?

关于c++ - 在 VS 2010 上临时接受回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4478107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com