If NOT_WORKING is defined, the following program gives me linker errors:
/usr/bin/ld: Undefined symbols:
Definer<int, 1>::d
Definer<int, 2>::d
Definer<int, 3>::d
Definer<int, 4>::d
collect2: ld returned 1 exit status
But if NOT_WORKING is not defined it compiles and runs. I can't figure out: 1) Why those things are not getting instanciated. 2) Why the second method accesses things that the first method does not. 3) How to get the LIST thingy in this forum to work:lol:
#define NOT_WORKING
#include <iostream>
template <typename C, int cnt>
struct Definer
{
static const C d = cnt - 1;
Definer<C, cnt-1> f;
C const& lookup(int index)
{
if (index == (cnt - 1))
return d;
return f.lookup(index);
}
};
template <typename C>
struct Definer <C, 0>
{
static const C d = 0;
C const& lookup(int index)
{
throw 18;
}
};
int main()
{
Definer<int, 4> g;
int i = g.f.f.f.d;
std::cout << i << std::endl;
int j = g.f.f.d;
std::cout << j << std::endl;
int k = g.f.d;
std::cout << k << std::endl;
int l = g.d;
std::cout << l << std::endl;
#ifdef NOT_WORKING
i = g.lookup(0);
std::cout << i << std::endl;
j = g.lookup(1);
std::cout << j << std::endl;
k = g.lookup(2);
std::cout << k << std::endl;
l = g.lookup(3);
std::cout << l << std::endl;
#endif
return 0;
}