将 Lambda 与 SIGC 库结合使用
use lambda in connection with sigc library
我想在 gtk++ 中使用与 goocanvas 相关的 lambda 表达式。据我了解,这意味着我必须能够将我的 lambda 放入 sigc++ 函子中。
我尝试了类似的东西:
sigc::slot<bool, const Glib::RefPtr<Goocanvas::Item>& , GdkEventMotion* > slot2=
[]( const Glib::RefPtr<Goocanvas::Item>& item, GdkEventMotion* ev)->bool
{
cout << "Lambda " << endl; return false;
};
((Glib::RefPtr<Goocanvas::Item>&)item1)->signal_motion_notify_event().connect( slot2);
但这不会编译。
是否有机会让 sigc 直接使用 lambda 或更好的 gtkmm 工作,而无需 sigc++ 中间体:-)
对于没有参数的 void 返回函数/方法,非常简单,例如(gcc 4.6, 4.7):
fileButton.signal_pressed().connect([this]{on_file_button_clicked();});
不幸的是,我无法获得值返回或参数获取方法来编译,并且不得不求助于sigc::mem_fun()
。 最近似乎有一些活动可以解决此问题,例如,此提交。 如果您有 sigc++ 版本 2.2.11 或更高版本,您可以尝试定义 SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
,但我不知道它的效果如何。
与此相关的还有此错误报告。
我发现以下代码被截断了,可以完成这项工作。我不知道它如何与 sigc++ 库交互,但我可以在简单情况下使用它。也许其他人可以看看它。
#include <type_traits>
#include <sigc++/sigc++.h>
namespace sigc
{
template <typename Functor>
struct functor_trait<Functor, false>
{
typedef decltype (::sigc::mem_fun (std::declval<Functor&> (),
&Functor::operator())) _intermediate;
typedef typename _intermediate::result_type result_type;
typedef Functor functor_type;
};
}
更新:Libsigc 现在无需任何额外的用户代码即可处理 lambas。如果使用任何当前版本,则必须删除上述代码。
使用 gtkmm helloworld 示例的演示:
#include "helloworld.h"
#include <iostream>
HelloWorld::HelloWorld()
: m_button("Hello World") // creates a new button with label "Hello World".
{
// Sets the border width of the window.
set_border_width(10);
// When the button receives the "clicked" signal, it will call the
// on_button_clicked() method defined below.
m_button.signal_clicked().connect(
sigc::mem_fun(*this, &HelloWorld::on_button_clicked));
m_button.signal_clicked().connect([=]() { std::cout << "hello lambdan"; });
// This packs the button into the Window (a container).
add(m_button);
// The final step is to display this newly created widget...
m_button.show();
}
HelloWorld::~HelloWorld() {}
void HelloWorld::on_button_clicked() {
std::cout << "hello method" << std::endl;
}
Lambda有效。
相关文章:
- 多态性和功能结合
- 将 Vulkan 与 SFML 结合使用?
- 将 c++ 生成器应用程序与外部 dll 结合使用
- 如何将增强MSM与增强信号结合使用?
- MS Visual Studio 解决方案结合了非托管C++项目和 C# 项目
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 将预编译的 C 共享库与 JNI/NDK 结合使用
- C++ 将函数指针与最佳性能相结合
- 将 Lua 与 C++ DLL 结合使用
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 将 aws-iot-device-sdk-cpp-v2 与 CMake 结合使用
- 将2个欧拉角与GLM相结合的问题
- 结合std::execution和OpenMP是否可取
- 抽象和派生与std::list相结合
- 想要将 CGAL 与四元数相结合是否合乎逻辑
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- 如何将 IAudioClient3 (WASAPI) 与实时工作队列 API 结合使用
- 可变参数模板与默认模板参数相结合
- 函数与返回类型 node* 与 C++ 中的 OOP 结合使用
- 将 Lambda 与 SIGC 库结合使用