Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2003-10-30 06:52:44


> I could reduce the linker problem to a template instantiation problem
> for static member variables when using tthe BN trick. The following
> code:
>
> template<class D>
> struct A {
> const D& derived() const {return *static_cast<const D*> (this);}
> };
>
> template<class T>
> class B : public A<B<T> > {
> public:
> T f() const { return zero_; }
> private:
> static T zero_;
> };
>
> template<class T>
> T B<T>::zero_ = 0;
>
> int main()
> {
> B<int> matrix;
> return matrix.derived().f();
> }
>
> when compiled with g++-3.3.2 (at least under MacOS X) gives the
> following linker error:
>
> /usr/bin/ld: Undefined symbols:
> B<int>::zero_
> collect2: ld returned 1 exit status
>
> This causes problems with the uBlas sparse matrices. Sometimes a
> workaround, explicitly defining the static member a second time will
> work.

I think that's a related problem to:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11026

It's been classified as a request for a new feature - apparently gcc on
apple has never supported weak symbols so static data members of templates
never get instantiated :-(

However using a getter function should work:

 template<class T>
class B : public A<B<T> > {
public:
   T f() const
{
    static const T zero_;
    return zero_;
}
};

John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk