Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1297|回复: 1
打印 上一主题 下一主题

C++11新特性之std stl

[复制链接]

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
跳转到指定楼层
楼主
发表于 2020-3-2 11:42:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 Qter 于 2020-3-6 11:16 编辑
  1. std::forward<Parent>(parent),
  2. std::move

  3. std::make_unique<ContactsBoxController>()


  4. Ui::show(Box<PeerListBox>(std::make_unique<ContactsBoxController>(), [](not_null<PeerListBox*> box) {
  5.                                 box->addButton(langFactory(lng_close), [box] { box->closeBox(); });
  6.                                 box->addLeftButton(langFactory(lng_profile_add_contact), [] { App::wnd()->onShowAddContact(); });
  7.                         }));



  8. template <typename BoxType, typename ...Args>
  9. inline object_ptr<BoxType> Box(Args&&... args) {
  10.         auto parent = static_cast<QWidget*>(nullptr);
  11.         return object_ptr<BoxType>(parent, std::forward<Args>(args)...);
  12. }


  13. Fn<QString()> textFactory,
  14. std::move(textFactory),
  15. using Fn = std::function<Signature>;
  16. PeerListBox::PeerListBox(        QWidget*,        std::unique_ptr<PeerListController> controller,        Fn<void(not_null<PeerListBox*>)> init): _controller(std::move(controller)), _init(std::move(init)) {        Expects(_controller != nullptr);}
  17.         Fn<void(PeerListBox*)> _init;




复制代码
回复

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
沙发
 楼主| 发表于 2020-4-8 11:24:29 | 只看该作者
class A {void Test();
   void resultHandler(int result);

}


void A::Test()
        {
                auto addBox = Ui::show(Box<GroupBox>(), LayerOption::KeepOther);
                addBox->setResultHandler(std::bind(&ContactBox::resultHandler, this, -1));
        }



class GroupBox {
  void setResultHandler(Fn<void(int)> handler);
Fn<void(int)> _resultHandler{ nullptr };
}

        void GroupBox::setResultHandler(Fn<void(int)> handler)
        {
                _resultHandler = handler;
        }


注意事项:
  • 预绑定的参数是以值传递的形式
  • 不预绑定的参数要用std::placeholders(占位符)的形式占位,从_1开始,依次递增,是以引用传递的形式
  • std::bind的返回值是可调用实体,可以直接赋给std::function
  • 对于绑定的指针,引用类型参数,调用者需保证在调用之前生命周期还存在
  • std::placeholders表示新的可调用对象的第几个参数和原函数的第几个参数进行匹配




链接:https://www.jianshu.com/p/621fc81a1dc

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-3 10:32 , Processed in 0.055188 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表