Tool/software: TI C/C++ Compiler
Hi
I recently stumbled upon an odd compiler error. I was able to reduce the code to the following program that shows the same behavior:
#include <utility> using std::declval; template<typename F, typename T> struct Test { enum { value = sizeof(declval<F>()(declval<T>()), 0) }; // 1 // enum { value = sizeof(std::declval<F>()(std::declval<T>()), 0) }; // 2 }; template<typename F, typename T> void call(F&& f, T t) { (void) Test<F, T>::value; f(t); // not actually needed } int f() { int n = 0; call([&n](auto) { // 3 call( [&n](auto) { // 4 n += 1; }, 0); }, 0); return n; }
As given, the code compiles with at least gcc and clang, whereas the TI C6000 compiler (8.3.4) emits the following error:
"lam2.cc", line 23: error #20: identifier "n" is undefined n += 1; ^ detected during: instantiation of function "lambda [](auto)->auto [with <auto-1>=int]" at line 7 instantiation of class "Test<F, T> [with F=lambda [](auto)->auto, T=int]" at line 13 instantiation of "void call(F &&, T) [with F=lambda [](auto)->auto, T=int]" at line 13 instantiation of "void call(F &&, T) [with F=lambda [](auto)->auto, T=int]" at line 21 1 error detected in the compilation of "lam2.cc". >> Compilation failure
I made a number of observations that only make the problem seem weirder:
- If you comment out the line marked "1" and uncomment the line marked "2", the code compiles
- If you replace one of the auto parameter types in either line "3" or "4" with "int", the code compiles
- If you add a local reference "auto& nref=n;" in the lambda expression on line "3" and capture "nref" instead of "n" in line "4", the code compiles
All of that seems really odd to me, you might want to have a look at it.
Markus