C++核心準則C.170: 如果感覺需要重載lambda表達式,使用泛型lambda表達式

C.170: If you feel like overloading a lambda, use a generic lambda

C.170: 如果需要重載lambda表達式,使用泛型lambda表達式

 

Reason(原因)

You cannot overload by defining two different lambdas with the same name.

你無法以爲兩個不同的lambda表達式取相同名字的方式來實現重載。

 

Example(示例)

void f(int);
void f(double);
auto f = [](char);   // error: cannot overload variable and function

auto g = [](int) { /* ... */ };
auto g = [](double) { /* ... */ };   // error: cannot overload variables

auto h = [](auto) { /* ... */ };   // OK

 

Enforcement(實施建議)

The compiler catches the attempt to overload a lambda.

 

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c170-if-you-feel-like-overloading-a-lambda-use-a-generic-lambda

 


 

覺得本文有幫助?歡迎點贊並分享給更多的人。

閱讀更多更新文章,請關注微信公衆號【面向對象思考】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章