Template Bug?

Perry Smith pedz@easesoftware.com
Thu Nov 9 16:32:00 GMT 2006


I believe the problem is in my code but I'm lost.  The g++ compiler  
on the Mac exhibits behavior different from the other compilers.

I'm using three compilers to test this with:

g++ --version
powerpc-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc.  
build 5250)

g++ --version (on AIX 5.3)
g++ (GCC) 4.0.2

xlC -qversion
IBM XL C/C++ Enterprise Edition V8.0 for AIX
Version: 08.00.0000.0000

On the Mac, the program compiles and runs as written.  If NOT_WORKING  
is defined, then I get link errors:

/usr/bin/ld: Undefined symbols:
Definer<int, 2>::d
Definer<int, 3>::d
Definer<int, 4>::d
collect2: ld returned 1 exit status
make: *** [temp] Error 1

With the other two compilers, I get link errors either way.

Program Starts here:

#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;

#ifdef NOT_WORKING

     int i = g.lookup(0);
     std::cout << i << std::endl;

     int j = g.lookup(1);
     std::cout << j << std::endl;

     int k = g.lookup(2);
     std::cout << k << std::endl;

     int l = g.lookup(3);
     std::cout << l << std::endl;

#else

     int i = g.lookup(0);
     int j = g.lookup(1);
     int k = g.lookup(2);
     int l = g.lookup(3);

     std::cout << i << std::endl;
     std::cout << j << std::endl;
     std::cout << k << std::endl;
     std::cout << l << std::endl;

#endif

     return 0;
}



More information about the Gcc-help mailing list