模板类成员方法内的自动 lambda:不命名类型
auto lambda inside template class member method: does not name a type
为什么我无法编译以下代码(使用 g++ 4.7.1)?
#include <set>
template<typename T>
class Problem
{
public:
void f();
std::set<int> dummyvalue;
};
template<typename T>
void Problem<T>::f()
{
auto mytestlambda = [this](){
dummyvalue.clear();
};
}
int main()
{
return 0;
}
我收到以下错误:
main.cpp: In member function 'void Problem<T>::f()':
main.cpp:17:10: error: 'mytestlambda' does not name a type
我在检查无法调用"clear()"方法的问题时遇到了这个问题。
添加'-std=c++11'真的让我解决了我真正的问题:
main.cpp: In lambda function:
main.cpp:18:26: error: no matching function for call to 'std::set<int>::clear() const'
main.cpp:18:26: note: candidate is:
In file included from /usr/include/c++/4.7/set:61:0,
from main.cpp:1:
/usr/include/c++/4.7/bits/stl_set.h:580:7: note: void std::set<_Key, _Compare, _Alloc>::clear() [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>] <near match>
/usr/include/c++/4.7/bits/stl_set.h:580:7: note: no known conversion for implicit 'this' parameter from 'const std::set<int>*' to 'std::set<int>*'
为什么这里涉及"康斯特"?
这是
GCC 4.7中的一个错误。升级到 4.8 可以修复它。
显然,GCC 4.7错误地将this
捕获为const
。
这是因为您在编译器中没有启用C++11。尝试使用 GCC --std=c++11
标志。然后它应该编译正确。
尝试使用 -std=c++11
编译。可悲的是,当前的编译器仍然默认使用>10年的标准。
相关文章:
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 在 lambda 捕获中声明的变量的类型推导
- C++ 模板类型的静态 lambda 成员的构造
- Clang 工具,用于提取给定 lambda 类型的 lambda 主体
- 如果 lambda 没有指定的类型,std::function 如何接受 lambda?
- Lambda可以用作非类型模板参数吗
- 如何确定捕获不可复制参数的 lambda 的类型?
- 如何制作可以接受任何类型的参数的 std::函数和 lambda
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 运算符中的不同类型? 具有无捕获,相同的签名,lambda
- 通过参数传递 lambda(无函数类型模板)
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- C++ 中的 Java8 lambda 类型比较
- 推断衰减到函数指针的 lambda 类型
- 从 lambda 内部返回 C++11 中的 lambda 类型(平面映射函数)
- 如何强制编译器识别模板化函数中的lambda类型
- 初始化和 lambda 类型参数
- 具有相同签名和捕获的常见 lambda 类型
- 我可以在std::函数中使用模板类型T来传递lambda类型的函数C++11吗
- C++ upper_bound 对 <lambda 类型的对象没有匹配的函数调用