The following code fails to compile in gcc 4.6.3, while it compiles flawless in clang 3.1 (I provide two versions of the class DummyMath, both exhibit the same problem):
#include <iostream>
using namespace std;
//* // To swap versions, comment the first slash.
// ===============================================
// V1
// ===============================================
template <typename T>
class DummyMath
{
public:
T sum(T a1, T a2);
T mult(T a1, int n);
};
template <typename T>
T DummyMath<T>::sum(T a1, T a2)
{
return a1 + a2;
}
template <typename T>
T DummyMath<T>::mult(T a1, int n)
{
auto x2 = [this](T a1) -> T
{
return sum(a1, a1); // <------- gcc will say that "sum" was not declared in this scope!
};
T result = 0;
n = n/2;
for(int i = 0; i < n; ++i)
result += x2(a1);
return result;
}
/*/
// ===============================================
// V2
// ===============================================
template <typename T>
class DummyMath
{
public:
T sum(T a1, T a2)
{
return a1 + a2;
}
T mult(T a1, int n)
{
auto x2 = [this](T a1) -> T {
return sum(a1, a1);
};
T result = 0;
n = n/2;
for(int i = 0; i < n; ++i)
result += x2(a1);
return result;
}
};
//*/
int main()
{
DummyMath<float> math;
cout << math.mult(2.f, 4) << endl;
return 0;
}
The error is:
main.cpp:25:20: error: ‘sum’ was not declared in this scope
Both versions (V1 and V2) of class DummyMath fail in gcc, and both succeed in clang. Is that a bug in GCC?
Thanks.