Захват this-указателя в лямбда-оболочке вокруг рекурсивной функции
У меня есть шаблон классаWrap<T>
с рекурсивной функцией-членомtest(int)
что я хочу перейти на алгоритм STL с лямбда (std::accumulate
в коде ниже).
Если я использую список захвата по умолчанию=
и сделай мою рекурсивную функцию meberstatic
, все в порядке и получить результат, который я хочу.
Однако, если я сделаю это нестатической функцией-членом, и Visual C ++, и gcc 4.7.2 пожалуются на унифицированнуюthis
, если я не квалифицирую свой рекурсивный вызов какthis->test()
.
#include <algorithm>
#include <iostream>
#include <vector>
template<typename T>
struct Wrap
{
static int test1(int depth)
{
std::vector<int> v = { 0, 1, 2, 3 };
return depth == 0? 1 : std::accumulate(v.begin(), v.end(), int(0), [=](int sub, int const&) {
return sub + test1(depth - 1);
});
}
int test2(int depth)
{
std::vector<int> v = { 0, 1, 2, 3 };
return depth == 0? 1 : std::accumulate(v.begin(), v.end(), int(0), [=](int sub, int const&) {
return sub + /*this->*/test2(depth - 1);
});
}
};
int main()
{
std::cout << Wrap<int>::test1(0) << "\n"; // 1
std::cout << Wrap<int>::test1(1) << "\n"; // 4
std::cout << Wrap<int>::test1(2) << "\n"; // 16
Wrap<int> w;
std::cout << w.test2(0) << "\n"; // 1
std::cout << w.test2(1) << "\n"; // 4
std::cout << w.test2(2) << "\n"; // 16
}
Выход наLiveWorkSpace:
source.cpp: In instantiation of 'int Wrap<T>::test2(int) [with T = int]':
source.cpp:32:26: required from here
source.cpp:19:74: error: missing initializer for member 'Wrap<T>::test2(int) [with T = int]::<lambda(int, const int&)>::__this' [-Werror=missing-field-initializers]
Раскомментировать/*this->/*
кусок, дает тот же результат, что и для статической функции-члена.
Почему мне нужно квалифицировать мой рекурсивный вызовthis->
?