Passing a reference to static-const class member --> link error?

J

John Stiles

I have written some pretty simple code which works great in Dev Studio
and CodeWarrior, but gcc is giving me link errors. Here is what I've
been able to distill it down to:

namespace Private {
template <class T> struct lengthof;
template <class T, size_t N> struct lengthof<T[N]> { static const
size_t array_size = N; };
}

#define lengthof( x ) Private::lengthof<__typeof__(x)>::array_size

const size_t& Min(const size_t& a, const size_t& b) { return b < a ? b
: a; }

int main (int argc, char * const argv[])
{
char c[1];
printf( "%d", Min( 123, lengthof(c) ) );
}


Trying to build this with GCC4 (via Xcode), I get:
/usr/bin/ld: Undefined symbols:
Private::lengthof<char [1]>::array_size
collect2: ld returned 1 exit status


There are three separate workarounds I could find:
1 -- use "enum" instead of "static const size_t"
template <class T, size_t N> struct lengthof<T[N]> { enum { array_size
= N }; };

2 -- explicitly cast the array_size member to size_t before passing it
to Min
#define lengthof( x ) size_t(
Private::lengthof<__typeof__(x)>::array_size )

3 -- change Min to take values instead of references (the real
program's implementation is templated, however, and I don't want value
passing in the general case).

Why might this be happening? It smells kind of like a GCC bug to me.
 
J

John Stiles

One more thing I just found--switching to GCC 3.3 instead of 4.0 makes
the problem go away.
 
J

John Stiles

With the help of some C++ gurus I found the answer.
You need to explicitly provide a definition of lengthof::array_size.
Just declaring it in the class isn't enough:

namespace Private {
template <class T> struct lengthof;
template <class T, size_t N> struct lengthof<T[N]> { static
const size_t array_size = N; };
template <class T, size_t N> const size_t
lengthof<T[N]>::array_size;
}

Man, GCC4 is testing all of our C++ knowledge to the limits! :) Funny
how no other compiler needs this, and even GCC4 doesn't need it most of
the time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top