c++ - sigc++ 与 lambda 给出错误 : void value not ignored as it ought to be

标签 c++ c++11 libsigc++

我有一个连接到 c++11 lambda 的 libsigc++ 信号。

sigc::signal<void, std::string> foo;

foo.connect([](string s) { cout << s << endl; });

foo.emit(string("Hello"));

我想将信号的返回类型从 void 更改为非 void

sigc::signal<int, std::string> foo;

foo.connect([](string s) { return s.size(); });

cout << foo.emit(string("Hello")) << endl;

这会产生错误:

void value not ignored as it ought to be

这种使用模式可以用 lambda 实现吗?

最佳答案

请参阅此处提及的 SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE:https://developer.gnome.org/libsigc++/stable/group__slot.html#details

您只需将其放在源文件的顶部附近即可:

namespace sigc {
  SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}

希望我们在未来的仅 C++11 版本的 libsigc++ 中不再需要这样做。

您可能也想使用 sigc::track_obj():https://developer.gnome.org/libsigc++/unstable/group__track__obj.html#details

关于c++ - sigc++ 与 lambda 给出错误 : void value not ignored as it ought to be,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471257/

相关文章:

c++ - std::bind 与 std::shared_ptr

c++ - 指针的部分特化,C++

c++ - 当列数固定时,Eigen::无法将 Block<Derived> 转换为 Ref<Derived>

c++ - qmake 仅在 Debug模式下运行命令,如何?

c++ - 实现链表

c++ - 通过值可变捕获的 lambda 不适用于 const &?

c++ - 使用临时对象是否有性能优势?

c++ - sigc::mem_fun中的C++ “no match for call”错误

c++ - 编译问题 sigc++/gtkmm in own namespaces

c++ - libsigc 如何包含在 gtkmm 项目中?