Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing a reference to static-const class member --> link error?

Reply
Thread Tools

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

 
 
John Stiles
Guest
Posts: n/a
 
      06-02-2005
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.

 
Reply With Quote
 
 
 
 
John Stiles
Guest
Posts: n/a
 
      06-02-2005
One more thing I just found--switching to GCC 3.3 instead of 4.0 makes
the problem go away.

 
Reply With Quote
 
 
 
 
John Stiles
Guest
Posts: n/a
 
      06-02-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57