473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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.

Jul 23 '05 #1
2 1959
One more thing I just found--switching to GCC 3.3 instead of 4.0 makes
the problem go away.

Jul 23 '05 #2
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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rémi Peyronnet | last post by:
Hello, I have some link problems while using basic_string with another type (unsigned int in my case) This is a sample source : " #include <string>
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
0
by: michael | last post by:
How can I reference an instance of a static class member accross all translation units? I don't want to pass a reference between dlls. Lib1.dll PROJECT 1 A.h class A { public:
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
5
by: Marcin Gil | last post by:
Hi! I have the code like this (obvious things like ctor/dtor removed) typedef struct _NODE { int val; int index; } Node;
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
7
by: shaunb | last post by:
Hi. I am, hopefully, coming towards the end of my program, just need a little bit of help with this runtime error on my program: This appears when i run the debugger, appears when i get to...
5
emaghero
by: emaghero | last post by:
I have the following class declaration class class_name{ public: class_name(); // Constructor // Member functions void function_1(); void function_2(double *mat); void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.