[問題] std::function 可以疊幾層
看板C_and_CPP作者gino0717 (gino0717)時間6年前發表 (2019/02/22 14:10), 6年前編輯推噓5(5推 0噓 9→)留言14則, 6人參與, 6年前最新討論串1/1
最近在c++看到這東西,感覺很厲害,想試試看一直疊上去。
首先是
void foo(int)
{
std::cout << "fo is called with: " << i << "\n";
}
然後
using foType = std::function<void(int)>;
int main()
{
foType f1 = std::bind(&foo,std::placeholders::_1);
f1(5);
}
這樣會輸出 fo is called with: 5
這樣算一層。
如果加個
void foAndFo(foType fo)
{
std::function<void(int)> fos = std::bind(fo, std::placeholders::_1);
fos(1);
}
等於是外面再包一層,但是bind的地方該怎麼寫呢?似乎是不能
std::function<foType> fo2 =std::bind(&foAndFo, std::placeholders::_1);
這樣寫啊
想說一直疊上去結果疊兩層就沒。了
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.164.208
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1550844655.A.1EE.html
※ 編輯: gino0717 (123.194.164.208), 02/22/2019 22:11:17
推
02/22 22:28,
6年前
, 1F
02/22 22:28, 1F
感覺會出現一堆莫名其妙的東西
我用
auto fo2 =std::bind(&foAndFo, std::placeholders::_1, std::placeholders::_2);
疊到第二層了,然後把auto印出來fo2的type原型是
class std::_Binder<struct std::_Unforced, void(__cdecl*)(class
std::function<void __cdecl(int)>, int), struct std::_Ph<1> const &, struct
std::_Ph<2> const &>
哇
再往上疊還得了
※ 編輯: gino0717 (123.194.164.208), 02/22/2019 22:41:48
→
02/22 23:04,
6年前
, 2F
02/22 23:04, 2F
→
02/23 00:01,
6年前
, 3F
02/23 00:01, 3F
→
02/23 00:02,
6年前
, 4F
02/23 00:02, 4F
推
02/23 10:35,
6年前
, 5F
02/23 10:35, 5F
→
02/23 10:35,
6年前
, 6F
02/23 10:35, 6F
→
02/23 10:36,
6年前
, 7F
02/23 10:36, 7F
→
02/23 10:37,
6年前
, 8F
02/23 10:37, 8F
→
02/23 10:37,
6年前
, 9F
02/23 10:37, 9F
推
02/23 13:29,
6年前
, 10F
02/23 13:29, 10F
推
02/23 17:58,
6年前
, 11F
02/23 17:58, 11F
→
02/23 17:59,
6年前
, 12F
02/23 17:59, 12F
→
02/23 17:59,
6年前
, 13F
02/23 17:59, 13F
推
03/18 10:58,
6年前
, 14F
03/18 10:58, 14F