PTT
Submit
Submit
选择语言
正體中文
简体中文
PTT
C_and_CPP
Re: [问题] bind object(std::bind)内部用copy?
楼主:
Caesar08
(Caesar)
2016-07-14 21:45:46
: → PkmX: 可是bind那样写也是没办法处理func是吃rvalue ref的情况 07/14 16:26
: → PkmX: http://melpon.org/wandbox/permlink/i6hpL0VL93nEqzbz 07/14 17:17
: → PkmX: 这个版本用lambda可以达到你要的效果 建立时先复制args一次 07/14 17:18
: → PkmX: 然后因为只用一次 他会直接把复制的args move给func 07/14 17:19
: → PkmX: 但若func要lvalue ref 会用template版本转成lvalue ref给它 07/14 17:20
因为有点离题了,所以另开一篇文章
template<class Func,class ... Args>
auto test_lambda(Func &&func,Args &&...args)
{
return [=]() mutable{
func(move(args)...);
};
}
这是我之前写的方法
但是如果argument是array,那需要做decay(不然不能通过compile)
所以新的方法就变成
template<class T>
decay_t<T> decay_copy(T &&val)
{
return std::forward<decltype(val)>(val);
}
template<class Func,class ... Args>
auto test_decay_(Func &&func,Args &&...args)
{
return [&]{
std::move(func)(move(args)...);
};
}
template<class Func,class ... Args>
auto test_decay(Func &&func,Args &&...args)
{
return test_decay_(decay_copy(std::forward<decltype(func)>(func))
,decay_copy(std::forward<decltype(args)>(args)...));
}
其实我希望的作法是像thread的实作方式,也就是
INVOKE(decay_copy(std::forward<decltype(func)>(func))
,decay_copy(std::forward<decltype(args)>(args)...));
但是最后再传给func的时候是用move的方式去传的
可是上述的function有一个问题test_decay_的capture list是&
所以在呼叫test_decay的return value的时候
就会当掉了
阿如果我用test_lambda的方式,就不能像thread的实作方式一样了...
作者:
PkmX
(阿猫)
2016-07-14 23:41:00
为什么如果argument是array要做decay呢?
http://melpon.org/wandbox/permlink/pkQz2gti5DmeKBxI
这个应该是g++的bug?根据expr.prim.lambda/16应该是ok的
继续阅读
[问题] integer做完bit &后判断是否为0
darkhcv
[问题] error MSB6006
b26168
[问题] bind object(std::bind)内部用copy?
Caesar08
[问题] vs2015内存视窗乱码
a2975313
[问题] 如何使用minGW编译DLL (with def档)
whatai
[闲聊] Linus Torvalds对于注解的看法
wtchen
[问题] 出现MAX was not declared in the scope
c2147369
[问题] readb/readw/readl/readq
loach98
Fw: [问题] findwindow 抓不到vb写的元件
areyo
Re: [问题] 无法了解shell code执行程式
enonrick
Links
booklink
Contact Us: admin [ a t ] ucptt.com