C 11有了lambda後bind還有多大意義?

時間 2021-05-31 04:40:14

1樓:fe263

C++14已經廢棄bind了

....不好意思,bind沒被廢棄.不過bind在C++14起沒什麼卵用了.

我看到的唯一好用的地方是需要捕獲多個引數變成乙個無參呼叫的情況:

template

auto pack( F&& f,T&&... t)return [f=std::forward(f),args=make_tuple(std::

forward(t)...)](){

需要解tuple引數來使用

bind:

template

auto pack( F&& f,T&&... t)return bind(std::forward(f),std::forward(t)...);

2樓:梨梨喵

@徐辰提到了move capture是其中一點, 另外C++11 lambda無法處理型別多型, bind可以.

同樣的C++14就沒有這個問題了.

例子如下:

如果我們有乙個可以容納任意型別的容器Any#include

#include

#include

struct

PollyTypePush

};int

main

()這種情況下Lambda無法處理不同型別的引數, PollyTypePushFunc只能使用std::bind實現.

如果是在C++14下可以用Polymorphic Lambda這麼寫:

auto

PollyTypePushFunc=[

&container](

auto

&&value);

附上Any的實現:

class

AnyAny():

ptr(

nullptr

)Any

(const

Any&

_that):

ptr(

_that

.clone

())Any

(Any

&&_that):

ptr(

_that

.ptr

)template

U>inline

bool

is()

const

template

U>inline

StorageType

&as()

const

template

U>inline

StorageType

&value

()const

template

U>inline

operatorU()

const

Any&

operator=(

const

Any&a)

Any&

operator=(

Any&&a)

~Any

()private

:class

ContainerBase

virtual

ContainerBase

*clone

()const=0

;};template

T>class

Container

:public

ContainerBase

inline

ContainerBase

*clone

()const

Tvalue;};

inline

ContainerBase

*clone

()const

ContainerBase

*ptr;};

c 11 有沒有辦法在lambda的內部表示lambda自身?有的話是怎麼個寫法呢?

mewiteor 不動點組合子?c 11不支援auto型別的引數,所以我只會這麼寫 include using namespace std int factorial int t int main int n cin n cout 小莊讀書 想不出怎麼在lambda內部表示自己,但回答用functi...

c 11 既然有auto了,為什麼又要有decltype呢?

小板凳 decltype 可以讓你獲得編譯期的型別。auto不能。這一點讓 decltype 有很多玩法。光是這樣說不好理解,具體舉乙個檢測編譯期乙個類是否包含乙個 foo 方法為例子,decltype 配合模板使用可以檢測幾乎任意方法是否實現 c 14 include for srd experi...

C 11 中 typedef 和 using 有什麼區別?

碧水溪風 說個真事,同事n年c 開發經驗,c 特別溜。有一次我拿typedef和 define反過來覆過去問他,一分鐘不到他就懵圈了,根本分不清typedef和 define該怎麼用了。 原子筆 using 是C 11用來擴充套件typedef 的,不在typedef上擴充套件是為了盡可能保持C語言...