voi*_*ter 9 c++ lambda g++ c++11
我创建了一个方便构建lambda函数的宏,使用它我可以迭代我编写的库中的张量对象.但是,嵌套这些宏似乎会导致GCC经历内部分段错误.在扩展编译器的预处理器输出并经历一些试验和错误之后,我发现原因似乎是在decltype类或结构的方法中声明的嵌套lambda函数的参数列表中的使用.下面是使用标准库的最小示例.
#include <iostream>
#include <type_traits>
template <class Iterator, class Func>
void for_each(const Iterator first, const Iterator last, Func func)
{
for (Iterator it = first; it != last; ++it) {
func(*it);
}
}
template <class T>
class helper
{
typedef typename T::size_type type;
};
template <class T>
class helper<T&>
{
typedef typename T::size_type type;
};
template <class T>
class helper<T*>
{
typedef typename T::size_type type;
};
struct bar
{
struct foo
{
typedef int size_type;
} foo_;
void test()
{
int arr[] = { 1, 2, 3 };
for_each(arr, arr + 3, [&](int i) {
/*
** XXX: The "typename ... type" segfaults g++!
*/
for_each(arr, arr + 3, [&](typename helper<decltype(foo_)>::type j) {
});
});
}
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -Wall -std=c++0x nested_lambda.cpp
nested_lambda.cpp: In lambda function:
nested_lambda.cpp:42:56: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.6/README.Bugs> for instructions.
Preprocessed source stored into /tmp/ccqYohFA.out file, please attach this to your bugreport.
Run Code Online (Sandbox Code Playgroud)
我最初选择使用decltype因为一个对象被传递给一个宏,我需要提取对象的类型.从对象的类型(T,T&或T*),我将使用traits类拉取T::size_type. size_type然后是lambda函数参数的类型.
如何在不必使用typedef提前声明lambda函数参数的类型的情况下绕过这个问题?如果你能想到一些其他可以在宏中轻松实现的解决方案(即在lambda函数的参数列表中重复复制和粘贴),那也可以.
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |