c – 可变模板中的Lambdas

c – 可变模板中的Lambdas,第1张

概述使用Microsoft Visual C 2013(12.0),在可变模板中的构造函数中使用lambda时遇到编译时错误.我已经设法将其关闭,如下所示(请参阅错误评论的行).它似乎是12.0中的一个错误,在14.0中不存在.我还没有尝试其他版本.有没有关于这个bug的文档,也许是以发布说明的形式阐明了这个bug发生的条件,哪些说明它已经被明确地解决了? #include <functional> 使用Microsoft Visual C 2013(12.0),在可变模板中的构造函数中使用lambda时遇到编译时错误.我已经设法将其关闭,如下所示(请参阅错误评论的行).它似乎是12.0中的一个错误,在14.0中不存在.我还没有尝试其他版本.有没有关于这个BUG的文档,也许是以发布说明的形式阐明了这个BUG发生的条件,哪些说明它已经被明确地解决了? @H_419_2@#include <functional>// a simple method that can take a lambdavoID MyFunction(const std::function<voID()>& f) {}// a simple class that can take a lambdaclass MyClass{public: MyClass(const std::function<voID()>& f) {}};// non-templated testvoID test1(){ MyFunction([] {}); // OK MyClass([] {}); // OK MyClass o([] {}); // OK}// non-variadic template testtemplate<typename T>voID test2(){ MyFunction([] {}); // OK MyClass([] {}); // OK MyClass o([] {}); // OK}// variadic template testtemplate<typename... T>voID test3(){ MyFunction([] {}); // OK MyClass([] {}); // OK MyClass a([] {}); // error C4430: missing type specifIEr - int assumed. Note: C++ does not support default-int // error C2440: 'initializing' : cannot convert from 'test3::<lambda_12595f14a5437138aca1906ad0f32cb0>' to 'int' MyClass b(([] {})); // putting the lambda in an extra () seems to fix the problem}// a function using the templates above must be presentint main(){ test1(); test2<int>(); test3<int,int,int>(); return 1;}解决方法 截至今天(根据 CppCoreGuidelines),您应该使用{}括号初始化程序.你试了吗? @H_419_2@MyClass a{[] {}}; 总结

以上是内存溢出为你收集整理的c – 可变模板中的Lambdas全部内容,希望文章能够帮你解决c – 可变模板中的Lambdas所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存